简体   繁体   中英

java: Runtime.exec() Thread and errorOutput, readLine

I got very common thread hang condition although I don't understand why it happens. I have 2 threads: main() and errorStreamReaderThread


- execute external process via Runtime.exec()
- create new errorStreamReaderThread to consume error output stream
- perform manipulations on input and output streams of external process( execute commands and read output of the commands)
- send exit command to external process
- set interrupt flag for errorStreamReaderThread
- waitFor() for external process to terminate
- return rc of the external process


- perform Buffered readLine on error stream while !interruptedFlag &&(line=br.readLine())!=null (errorStream is passed from external process from main)
- perform log error stream


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.

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)

Additional environment factor is that this java class is executed as oracle java function (it is wrapped in the oracle)

Please let me know what I'm missing here and why I got this readLine hangs on the non existing errorStream

thank you

in fact the error stream for this buffered reader is no longer exists, since external process is finished

Do you guess that this is the case because you have sent an exit command to the external process, or have you really checked both cases?

Since BufferedReader.readLine() will no longer block but return null once the socket closes, I don't think the socket has closed...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM