Proper Use of ProcessBuilder

After researching, I noticed that the β€œright” way to use java ProcessBuilder is to create two more threads to handle the stdout / stderr of the newly created process so that it doesn't hang, as shown here: javaworld article

But this made me wonder about two questions - 1.) Why are separate processes necessary instead of the parent process gobbling stdout and then stderr sequentially?

2.) Also, if you redirected the threads to both exits to stdout, would it be acceptable only for the parent process to swallow the stdout stream and then not have to worry about deadlocks?

+3
source share
1 answer

Pay attention to your conditions. Threads are not processes .

  • Since the child can write for both, and you get a dead end when the buffer for is stderrfull (the child waits for the parent to read stderr, the parent waits for the child to close stdout).

  • No. If the child process is also required stdin, then you must process stdinin its main stream and read the combined output streams through the additional stream, otherwise you may have deadlocks (the child waits for the parent to read the output stream and the parent waits for the child to read the data on stdin).

+3
source

Source: https://habr.com/ru/post/1750316/


All Articles