问题描述
我在unix上运行Java进程。
I am running a Java process on unix.
我需要运行一个外部进程,该进程由主进程使用ProcessBuilder生成。主进程等待外部进程完成,然后生成下一个外部进程。我一直在这里工作。
I need to run an external process which is spawned by the main process using ProcessBuilder. The main process waits until the external process is done, and then spawnes the next external process. I have got it working till here.
public static void main(String [] args){
for(...){
int exitVal = runExternalProcess(args);
if(exitVal!= 0){
failedProcess.add(args);
}
}
}
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);
流程proc = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
String line = null;
while((line = br.readLine())!= null)
LOG.log(Level.SEVERE,line);
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;
}
我无法弄清楚的是,如何做到这一点。对于某些输入,外部进程挂起,在这种情况下,我想等待设置的超时时间,如果外部进程未完成,那么只需将其终止并将控制权返回给主进程(以及退出值,以便我可以跟踪失败的进程),以便可以启动下一个外部进程。
What I am not able to figure out is, how to do this. For some inputs the external process hangs and in such cases, I want to wait for a set timeout period and if the external process is not complete by then, just kill it and return control to the main process (along with exit value so that I can track the failed processes), so that the next external process can be launched.
我尝试使用proc.wait(TIMEOUT)然后使用proc.exitValue();获取退出值,但无法使其正常工作。
I tried using proc.wait(TIMEOUT) and then using proc.exitValue(); to get the exit value, but could not get it working.
谢谢!
推荐答案
你可以做Thread.join(long)或Thread.join(long,int)并在单独的线程中启动进程。
YOu could do Thread.join(long) or Thread.join(long, int) and start process in separate thread.
添加一些代码。 (工作但未完全测试所有极端情况)
Adding some code. (Works but Not fully tested with all corner cases)
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();
}
}
}
}
这篇关于使父线程等到子线程完成或超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!