终止 java 子进程的理想方式是什么? process.destroyForcibly() 不会终止我的进程

What is the ideal way to terminate a java subprocess? process.destroyForcibly() doesn't terminate my process

我有一个 Java Swing 应用程序,它创建了一个子进程。主 Swing 应用程序有一个停止按钮,当点击该按钮时应立即终止子进程。 "process.destroy()" 无效。

Process myProcess = new ProcessBuilder("java", "-classpath", System.getProperty("java.class.path"), "MyClass.java");
try {
    myProcess.waitFor();
} 
catch (Exception e) {
    e1.printStackTrace();
}

...

myStopButton.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        myProcess.destroyForcibly(); 
        // myProcess is a really long and complex process. So I could not destroy that using process.destroyForcibly().
    }
}
  1. 我可以向子进程发送一些信号来调用 System.exit() 在内部?
  2. 我可以使用 "taskkill" 仅终止子进程吗?

这真的取决于你的 java 版本。

之前 Java 8

public abstract void destroy()

Kills the subprocess. The subprocess represented by this Process object is forcibly terminated.

Java 8

public abstract void destroy()

Kills the subprocess. Whether the subprocess represented by this Process object is forcibly terminated or not is implementation dependent.

public Process destroyForcibly()

Kills the subprocess. The subprocess represented by this Process object is forcibly terminated. The default implementation of this method invokes destroy() and so may not forcibly terminate the process. Concrete implementations of this class are strongly encouraged to override this method with a compliant implementation. Invoking this method on Process objects returned by ProcessBuilder.start() and Runtime.exec(java.lang.String) will forcibly terminate the process.

Note: The subprocess may not terminate immediately. i.e. isAlive() may return true for a brief period after destroyForcibly() is called. This method may be chained to waitFor() if needed.

Returns: the Process object representing the subprocess to be forcibly destroyed.

Java 9

public abstract void destroy​()

Kills the process. Whether the process represented by this Process object is normally terminated or not is implementation dependent. Forcible process destruction is defined as the immediate termination of a process, whereas normal termination allows the process to shut down cleanly. If the process is not alive, no action is taken. The CompletableFuture from onExit() is completed when the process has terminated.

public Process destroyForcibly​()

Kills the process forcibly. The process represented by this Process object is forcibly terminated. Forcible process destruction is defined as the immediate termination of a process, whereas normal termination allows the process to shut down cleanly. If the process is not alive, no action is taken. The CompletableFuture from onExit() is completed when the process has terminated.

Invoking this method on Process objects returned by ProcessBuilder.start() and Runtime.exec(java.lang.String) forcibly terminate the process.

API注:

The process may not terminate immediately. i.e. isAlive() may return true for a brief period after destroyForcibly() is called. This method may be chained to waitFor() if needed. Implementation Requirements: The default implementation of this method invokes destroy() and so may not forcibly terminate the process. Implementation Note: Concrete implementations of this class are strongly encouraged to override this method with a compliant implementation.

Returns:

the Process object representing the process forcibly destroyed