process.waitFor(timeout, timeUnit) 在指定时间后不退出进程
process.waitFor(timeout, timeUnit) does not quit the process after specified time
我正在尝试使用进程构建器在我的 java 应用程序中执行可视化基本脚本代码。由于用户提供的脚本可能无法及时完成执行,我想提供限制此执行时间的方法。在下面的代码中,您可以看到我的逻辑,但它并没有真正执行它应该执行的操作。我怎样才能使这个 waitfor 工作以限制执行时间?
private void run(String scriptFilePath) throws ScriptPluginException {
BufferedReader input = null;
BufferedReader error = null;
try {
ProcessBuilder p = new ProcessBuilder("cscript.exe", "//U", "\"" + scriptFilePath + "\"");
String path = "";
if (scriptFilePath.indexOf("/") != -1) {
path = scriptFilePath.substring(0, scriptFilePath.lastIndexOf("/"));
}
path += "/" + "tempvbsoutput.txt";
p.redirectOutput(new File(path));
Process pp = p.start();
try {
pp.waitFor(executionTimeout, TimeUnit.MINUTES);
} catch (InterruptedException e) {
SystemLog.writeError(jobId, ScriptConsts.COMPONENT_ID, "VBScriptExecutor", "run", 80401104,
"VB Script executes fail.");
}
if (!pp.isAlive()) {
pp.getOutputStream().close();
}
// rest of the code flow
}
Process.waitFor(long, TimeUnit)
等到进程终止或经过指定的时间 (Javadoc)。 return 值表示进程是否退出。
if (process.waitFor(1, TimeUnit.MINUTES)) {
System.out.println("process exited");
} else {
System.out.println("process is still running");
}
waitFor()
超时后不终止进程。
如果要终止子进程,请使用 destroy() or destroyForcibly()。
我正在尝试使用进程构建器在我的 java 应用程序中执行可视化基本脚本代码。由于用户提供的脚本可能无法及时完成执行,我想提供限制此执行时间的方法。在下面的代码中,您可以看到我的逻辑,但它并没有真正执行它应该执行的操作。我怎样才能使这个 waitfor 工作以限制执行时间?
private void run(String scriptFilePath) throws ScriptPluginException {
BufferedReader input = null;
BufferedReader error = null;
try {
ProcessBuilder p = new ProcessBuilder("cscript.exe", "//U", "\"" + scriptFilePath + "\"");
String path = "";
if (scriptFilePath.indexOf("/") != -1) {
path = scriptFilePath.substring(0, scriptFilePath.lastIndexOf("/"));
}
path += "/" + "tempvbsoutput.txt";
p.redirectOutput(new File(path));
Process pp = p.start();
try {
pp.waitFor(executionTimeout, TimeUnit.MINUTES);
} catch (InterruptedException e) {
SystemLog.writeError(jobId, ScriptConsts.COMPONENT_ID, "VBScriptExecutor", "run", 80401104,
"VB Script executes fail.");
}
if (!pp.isAlive()) {
pp.getOutputStream().close();
}
// rest of the code flow
}
Process.waitFor(long, TimeUnit)
等到进程终止或经过指定的时间 (Javadoc)。 return 值表示进程是否退出。
if (process.waitFor(1, TimeUnit.MINUTES)) {
System.out.println("process exited");
} else {
System.out.println("process is still running");
}
waitFor()
超时后不终止进程。
如果要终止子进程,请使用 destroy() or destroyForcibly()。