我在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()。获得退出价值,但不能得到它的工作。
谢谢!
Process.wait(超时)是实际上可以从对象类的wait()方法,不应该在这种情况下 – naikus 2010-07-20 18:14:14