我有一个带有 2 个进程 java 的程序:processA 和 processB(2 个进程 java.exe 不是 2 个线程)。我使用下面的代码块从进程 A 调用 processB 此代码包含在下面的 RunTask 类中
public class RunTask implements Callable<Object> {
private String runParams;
public String getRunParams() {
return runParams;
}
public void setRunParams(String runParams) {
this.runParams = runParams;
}
@Override
public Object call() throws Exception {
try {
//System.out.println("run:" + runParams);
Process procB = Runtime.getRuntime().exec("java -jar processB.jar);
DataInputStream ls_in = new DataInputStream(procB.getInputStream());
String ls_str;
while ((ls_str = ls_in.readLine()) != null) {
System.out.println(ls_str);
}
} catch (Exception exp) {
exp.printStackTrace();
}
return null;
}
}
和主类我使用执行器
ExecutorService eservice = Executors.newSingleThreadExecutor();
while (1 == 1) {
String stringParams = getFilesNeedToImportAsString();
if (stringParams.trim().isEmpty()) {
long l1 = System.currentTimeMillis() - l;
System.out.println("all time" + l1 / 1000);
System.exit(100);
}
RunTask runTask = new RunTask();
runTask.setRunParams(SystemInfo.RUN_COMMAND + stringParams);
Future<Object> objectFuture = eservice.submit(runTask);
while (!objectFuture.isDone()) {
System.out.println("waiting the task running");
Thread.sleep(500);
}
}
但是当 processB 发生异常时,两个进程(processA,processB)似乎都被停止了,这是在 processB 上运行的代码
public Object call() {
try {
MutationResult result = mutator.execute();
return "ok";
} catch (Exception exp) {
exp.printStackTrace();
System.out.println("error on " + Thread.currentThread().getName() + "failed begin retry " + (++retryCount));
call();
System.out.println(retryCount + " completed");
return "ok";
}
}
如果我单独运行processB(通过命令行)它永远不会发生,或者当这个问题发生时我使用taskmanager杀死proceesA(被调用者),processB继续运行
请任何人给我这个问题的解决方案!