You need to worry about two things:
- Whether the process will block, trying to write to stdout, since you do not consume it, and
- How does a process interpret threads closing?
Closing the output stream will not explicitly cause deadlocks if the process does not require input. However, the process will detect that the end of the thread has been reached, and may terminate accordingly. Take grep
for example. This program will end when you close its input stream.
The easiest and safest way to ignore input will be to create a library that consumes and discards input in a separate stream. You've probably seen the Floating StreamGobbler
class that does just that.
Edit
So, I tried the following with two different subprocess programs:
Process proc = new ProcessBuilder(appStr).start(); proc.getInputStream().close(); InputStream eos = proc.getErrorStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(eos)); for ( String line = null; (line = reader.readLine()) != null; ) { System.err.println(line); } System.out.println(proc.waitFor());
The first time I wrote out a simple Java application:
public class App { public static void main(String[] args) throws Exception { for ( int i = 0; i < 1000; i++ ) { System.out.println(i); } } }
The process terminated normally, and the parent process completed displaying exit code 0
.
But then I tried to call it against the native program, in this case variations of cat
, an echo of the file. In this case, the child process failed and wrote a garbled message to the error stream, complaining that the standard output stream was closed. It aborts abnormally with a nonzero exit code.
So really what you offer when closing threads will not be successful in the general case. You should know that a child process can gracefully handle output stream loss, and I suspect that most applications will not. My intuition is that Java can make a graceful crash when the output stream is closed, so from a software point of view the stream is still open, no one is listening.
source share