Java中的Process Builder和Process - 如何使用超时执行进程:

mhs*_*ams 5 java process

我需要在具有特定超时的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还在等待进程完成?

Paŭ*_*ann 8

这是Java 在Windows上的实现中的一个错误Process.destroy().问题是批处理脚本(或其执行的shell)被终止,但不会杀死它自己的子进程(ping这里).因此,ping仍在运行之后.destroy(),也就是之后运行.waitFor().但不知何故,VM仍然等待ping完成才能完成.

看起来你无法从Java端做到这一点,无法可靠地杀死ping.

您可以考虑使用start(在批处理脚本或外部)将ping作为单独的进程调用.

(另见前面的讨论.)

或者改为类似unix的操作系统.