title: java: Runtime.exec() Thread and errorOutput, readLine
body: <p>I got very common thread hang condition although I don't understand why it happens.
  I have 2 threads: main() and errorStreamReaderThread</p>
  
  <p><b>In the main thread:</b><br>
  - execute external process via Runtime.exec()<br>
  - create new errorStreamReaderThread to consume error output stream<br>
  - perform manipulations on input and output streams of external process( execute commands and read output of the commands)<br>
  - send exit command to external process<br>
  - set interrupt flag for errorStreamReaderThread<br>
  - waitFor() for external process to terminate<br>
  - return rc of the external process<br></p>
  
  <p><b>In the errorStreamReaderThread:</b><br>
  - perform Buffered readLine on error stream while !interruptedFlag &amp;&amp;(line=br.readLine())!=null (errorStream is passed from external process from main)<br>
  - perform log error stream<br></p>
  
  <hr>
  
  <p>I expect that even though readLine() block on stream - as soon as originator of this stream is finished (external process in this case) this readLine should be interrupted.</p>
  
  <p>Observed behavior 
  - everything works fine 95% of the time - no hangs, both threads: main and error threads are finished and program finished.
  - about 5% it hangs
  - main thread is finished (waitFor is returned)
  - errorStreamReaderThread is blocked on BufferedReader.readLine (in fact the error stream for this buffered reader is no longer exists, since external process is finished)</p>
  
  <p>Additional environment factor is that this java class is executed as oracle java function (it is wrapped in the oracle)</p>
  
  <p>Please let me know what I'm missing here and why I got this readLine hangs on the non existing errorStream</p>
  
  <p>thank you</p>