2010-07-20 82 views
2

我在UNIX上运行Java进程。让父线程等到子线程完成或超时

我需要运行使用ProcessBuilder由主进程产生的外部进程。主进程等待,直到外部进程完成,然后产生下一个外部进程。我已经在这里工作了。

public static void main(String[] args) { for(...) { int exitVal = runExternalProcess(args); if(exitVal !=0) { failedProcess.add(args); } } }

private int runExternalProcess(String[] args) { ProcessBuilder pb = new ProcessBuilder(args[0], args[1], args[2]); pb.redirectErrorStream(true); Process proc = pb.start(); BufferedReader br = new BufferedReader(new InputStreamReader( proc.getInputStream())); String line = null; while ((line = br.readLine()) != null) LOG.log(Level.SEVERE, line);

//Main thread waits for external process to complete. 
//What I need to do is. 
// If proc.executionTime() > TIMEOUT 
//  kill proc;   
int exitVal = proc.waitFor(); 

proc.getInputStream().close(); 
proc.getOutputStream().close(); 
proc.getErrorStream().close(); 

return exitVal; 

}

我什么不能出来就是,如何做到这一点的身影。对于某些输入来说,外部进程会挂起,在这种情况下,我想等待一段设定的超时时间,如果外部进程没有完成,只需将其终止并将控制权返回给主进程(与退出值一起),以便我可以跟踪失败的进程),以便下一个外部进程可以启动。我尝试使用proc.wait(超时),然后使用proc.exitValue();我试过使用proc.wait(超时),然后使用proc.exitValue()。获得退出价值,但不能得到它的工作。

谢谢!

+0

Process.wait(超时)是实际上可以从对象类的wait()方法,不应该在这种情况下 – naikus 2010-07-20 18:14:14

回答

5

你可以做Thread.join(long)或Thread.join(long,int)并在单独的线程中启动进程。

添加一些代码。 (运作,但并不完全符合所有角落的情况下测试)

public class Test { 

    public static void main(String[] args) throws Throwable { 
     for(int i = 0; i < 3; i++) { 
      ProcessRunner pr = new ProcessRunner(args); 
      pr.start(); 
      // wait for 100 ms 
      pr.join(100); 
      // process still going on? kill it! 
      if(!pr.isDone()) { 
       System.err.println("Destroying process " + pr); 
       pr.abort(); 
      } 
     } 
    } 

    static class ProcessRunner extends Thread { 
     ProcessBuilder b; 
     Process p; 
     boolean done = false; 

     ProcessRunner(String[] args) { 
      super("ProcessRunner " + args); // got lazy here :D 
      b = new ProcessBuilder(args); 
     } 

     public void run() { 
      try { 
       p = b.start(); 

       // Do your buffered reader and readline stuff here 

       // wait for the process to complete 
       p.waitFor(); 
      }catch(Exception e) { 
       System.err.println(e.getMessage()); 
      }finally { 
       // some cleanup code 
       done = true; 
      } 
     } 

     int exitValue() throws IllegalStateException { 
      if(p != null) { 
       return p.exitValue(); 
      }   
      throw new IllegalStateException("Process not started yet"); 
     } 

     boolean isDone() { 
      return done; 
     } 

     void abort() { 
      if(! isDone()) { 
       // do some cleanup first 
       p.destroy(); 
      } 
     } 
    } 
    } 
+0

试过这种使用,和它的作品!谢谢! – pkrish 2010-07-20 21:14:55