这是我一直在做的伪代码:
ProcessBuilder processBuilder = new ProcessBuilder(cmdToExecute);
Process process = processBuilder.start();
StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR");
StreamGobbler outStreamGobbler = new StreamGobbler(process.getInputStream(), "OUTPUT");
errorGobbler.start();
outStreamGobbler.start();
int exitValue = process.waitFor();
process.destroy();
我在使用时收到 InterruptedException
Process.waitFor()
命令。当它是一个单线程的 java 应用程序时很好。但现在它是一个多线程应用程序,我得到
java.lang.InterruptedException
在那条线上。Java 文档说:
抛出:InterruptedException - 如果当前线程在等待时被另一个线程中断,则等待结束并抛出 InterruptedException
我理解这意味着什么,因为 1 个线程可能正在等待,而另一个线程也想等待,但是我如何克服这个异常,并且我将能够使用 waitFor()。
任何帮助都会受到重视。我觉得没有必要发布代码,但如果你们需要,请告诉我。
ExecutorService 的代码:
while(...){
.......
execServ.execute(...);
}
execServ.shutdown();
while(!execServ.isTerminated()){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}