title: Process.waitFor(), threads, and InputStreams
body: <p>In pseudocode, here's what I'm doing:</p>
  <pre><code>Process proc = runtime.exec(command);
  processOutputStreamInThread(proc.getInputStream());
  processOutputStreamInThread(proc.getErrorStream());
  proc.waitFor()
  </code></pre>
  <p>However, sometimes <code>processOutputStreamInThread</code> doesn't see any output and sometimes it does.  Roughly, the method creates a <code>BufferedInputStream</code> of the command's output and sends it to a logger.</p>
  <p>Based on what I'm seeing, I'm guessing that <code>command</code> need not have all it's output dumped into the streams fed by <code>getInputStream()</code> and <code>getErrorStream()</code>, thus allowing the stream to be empty.</p>
  <p>The results of my trials are the following questions:</p>
  <p><strong>(1)</strong> Does <code>waitFor()</code> in <a href="http://java.sun.com/javase/6/docs/api/java/lang/Process.html" rel="noreferrer">java.lang.Process</a> require the executed program's output to have been read before it returns?</p>
  <p>The documentation only states:</p>
  <blockquote>
  <p>causes the current thread to wait, if necessary, until the process represented by this <code>Process</code> object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits.</p>
  </blockquote>
  <p><strong>(2)</strong> Under what conditions do the streams provided by <code>getInputStream</code> and <code>getErrorStream</code> need to be closed and/or are they closed automatically?</p>
  <p>The documentation only states:</p>
  <blockquote>
  <p>Gets the error stream of the subprocess. The stream obtains data piped from the error output stream of the process represented by this Process object.</p>
  <p>Implementation note: It is a good idea for the input stream to be buffered.</p>
  </blockquote>
  <p>One <a href="https://stackoverflow.com/questions/1129441/process-builder-waitfor-issue-and-open-file-limitations">user reports</a> that he had to close the streams himself, but I get an exception at least part of the time indicating that the stream is already closed when I attempt to do so.</p>
  <p><strong>Edit:</strong> changed <code>getOutputStream</code> to <code>getInputStream</code>, now present above.</p>
  <p><strong>Resolution:</strong> The problem ended up being that in certain cases the threads used to process  the output stream wouldn't run until after my very short-lived process had completed, resulting in the input stream giving me no data.  <code>waitFor</code> didn't do any waiting for the output of the executed program.  Rather, the program ran and terminated before any output could be gathered.</p>
  <p>I used threads because I'm not sure how much output I was going to get on standard error and standard output and I wanted to be able to process both simultaneously, without blocking one or the other should only one of them have data available.  But, because my threads can't consistently read the executed program's output, it's a non-solution.</p>
  <p>My final coded looked something like this:</p>
  <pre><code>ProcessBuilder pb = new ProcessBuilder(cmdargs);
  pb.redirectErrorStream(true);
  Process proc = pb.start();
  processOutputStream(proc.getInputStream());
  proc.waitFor()
  </code></pre>