开发者

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

开发者 https://www.devze.com 2023-04-02 02:28 出处:网络
I got very common thread hang condition although I don\'t understand why it happens. I have 2 threads: main() and errorStreamReaderThread

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

In the main thread:

- 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

In the errorStreamReaderThread:

- 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...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号