开发者

Correct Usage of ProcessBuilder

After researching I have noticed that the "correct" way to use java's ProcessBuilder is to spawn two other threads to manage gobbling up the stdout/stderr of the newly created process so that it doesn't hang as is shown here : javaworld article

But this has left me wondering about 2 questions- 1.) Why exactly are seperate processes needed instead of having the parent process gobble up the stdout and then sequentially the stderr?

2.) In addition, if you were to redirect the streams to both go to s开发者_如何学Gotdout would it be acceptable to just have the parent process swallow the stdout stream, and then not have to worry about deadlocks?


Mind your terms. Threads aren't processes.

  1. Because the child could write to both and you would get a deadlock when the buffer for stderr is full (child waits for parent to read stderr, parent waits for child to close stdout).

  2. No. If the child process also needs stdin, then you must handle stdin in your main thread and read the merged output streams via an extra thread or you could have deadlocks again (child waits for parent to read the output stream and the parent waits for the child to read the data on stdin).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜