我需要在具有特定超时的java中执行外部批处理文件.这意味着如果批处理执行时间超过指定的超时,我需要取消执行.
这是我写的示例代码:
public static void main(String[] args) throws IOException, InterruptedException {
ProcessBuilder p = new ProcessBuilder("c:\\wait.bat", "25"); // batch file execution will take 25 seconds.
final long l = System.currentTimeMillis();
System.out.println("starting..." + (System.currentTimeMillis() - l));
final Process command = p.start();
System.out.println("started..." + (System.currentTimeMillis() - l));
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
command.destroy();
}
}, 5000); // it will kill the process after 5 seconds (if it's not finished yet).
int i = command.waitFor();
t.cancel();
System.out.println("done..." + (System.currentTimeMillis() - l));
System.out.println("result : " + i);
System.out.println("Really Done..." + (System.currentTimeMillis() - l));
}
Run Code Online (Sandbox Code Playgroud)
批处理文件"wait.bat"是这样的:
@echo off
echo starting the process...
@ping 127.0.0.1 -n 2 -w 1000 > nul
@ping 127.0.0.1 -n %1% -w 1000> nul
echo process finished succesfully
@echo on
Run Code Online (Sandbox Code Playgroud)
正如您在代码中看到的那样,批处理文件将花费25秒完成(main方法中的第一行),并且Timer将在5秒后销毁该命令.
这是我的代码的输出:
starting...0
started...0
done...5000
result : 1
Really Done...5000
BUILD SUCCESSFUL (total time: 25 seconds)
Run Code Online (Sandbox Code Playgroud)
正如您在输出中看到的那样,最后一行("Really Done ...")在第5秒执行,但应用程序在25秒后完成.
我的问题是:即使我在我的计时器中调用了destroy方法,为什么jvm还在等待进程完成?
归档时间: |
|
查看次数: |
14267 次 |
最近记录: |