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.
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 readstderr
, parent waits for child to closestdout
).No. If the child process also needs
stdin
, then you must handlestdin
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 onstdin
).
精彩评论