title: In java, how to make a single thread to wait for a process to finish?
body: <p>Here is what I have:
  I have a thread that starts a process, this process is created by ProcessBuilder and starts a string command in terminal. There are many concurrent threads that do the same, but on different data.</p>
  
  <p>Here is what I want to do:
  I want to make a single thread (which started a process) to wait for that process until it finishes. I came up with two methods, and none is working.</p>
  
  <p>method 1: to use process.waitFor(); and this causes all concurrent threads to wait for one single process (usually the first one) to finish. The description of waitFor(); says that it makes a single thread to wait, but that's not what it does, it actually makes ALL threads to wait. Hence the program is not concurrent anymore.</p>
  
  <p>method 2: to run another thread that reads the piped stream out of that process, waits until there is stream, and then, runs the function that was supposed to run after that process. The drawback is that there are many threads right now so I prefer not to use this method. The other issue with this method is that I get confused that which of these properties of the process should I use? OutputStream, InputStream or ErrorStream?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>public class Thread1 extends Thread{
  private String[] incommand;    //this is the command for the process builder
  private String newoutputfile;
  InputStream ins = null;
  Reader r = null;
  BufferedReader br = null;
  ProcessBuilder pbtx = null;
  
  public Thread1(String[] incommand, String newoutputfile){
      super("Thread1");
      this.incommand = incommand;        
      this.newoutputfile = newoutputfile;
      this.pbtx = new ProcessBuilder();
  }
  @Override
  public void run(){
      try{                    
                  pbtx.command(incommand);                                     
                  Process ptx = pbtx.start();
                  //to make sure job is done
                  ptx.waitFor(); //problem is apparently here
                  // made sure job is done
                  //the next function is supposed to run after the process is finished                
                      rite();                    
                  //
              } catch (IOException ex){
                  System.out.println("exception in thread t1");
                  ex.printStackTrace();
              }
              catch (InterruptedException yo){
              System.out.println("exception in thread t1");
              }
              }
  </code></pre>
  
  <p>By the way, the process is a ffmpeg process, each process works on different video data (no data dependencies or race condition or what so ever here). All these thread1 threads are made and started by another main thread in another function (main). The operating system in Linux. The IDE is Netbeans (that's where I get the description of each function). I tried to keep the copy-pasted code as short as possible (for simplicity), so if you think that the code for other functions or the rest of the program is needed please inform me to paste them here.</p>
  
  <p>Thank you very much,</p>