title: Using a thread to capture process output
body: <p>I am using a thread to capture stream output from a process, and then outputting that stream to the eclipse console. The question I have is when to terminate the thread that is doing the stream output.</p>
  
  <pre><code>Thread t = new Thread(new Runnable(){
          private boolean isProcessDone(Process p)
          {
              //not sure what to do here
          }
          public void run()
          {
              Process p = Runtime.getRuntime().exec("executable with output");
              BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
              BufferedReader error = new BufferedReader (new InputStreamReader(p.getErrorStream()));
              while ( !isProcessDone(p) ) {
                  String line;
                  if( (line = input.readLine()) != null )
                  {
                      System.out.println(line);
                  }
                  if( (line = error.readLine()) != null )
                  {
                      System.out.println(line);
                  }
              }
              input.close();  
              error.close();      
          }
      });
      t.start();
  </code></pre>
  
  <p>My question is what belongs in the <code>isProcessDone()</code> function. The example I am basing this off of uses the stream's <code>ready()</code> function, but I am unclear whether this will work for programs that either std::err and std::out, but not both. I also tried using </p>
  
  <pre><code>try{
      p.exitValue();
      return true;
  }catch(IllegalThreadStateException e){}
  return false;
  </code></pre>
  
  <p>but then the thread finishes before the while loop has a chance to act on the streams, and the output is lost.</p>