Java 生成另一个进程,而不是等待它

Java 生成另一个进程,而不是等待它,java,process,exec,processbuilder,Java,Process,Exec,Processbuilder,我有两个java进程。我想通过ProcessBuilder从另一个进程启动其中一个进程 我的问题是我只想开始另一个过程。我不想等待它的任何结果或输出。 即启动第二个流程后,两个流程将独立运行。 我该怎么做?在ProcessBuilder上不调用waitFor?通过不使用输出流 注意:第二个过程不会产生输出,而是“永远”运行。实际上,这两个都是长时间运行的进程,根据文档,您是否使用process类的waitFor方法,如果是的话。那我想你不必这么做 取自 在单独的线程中使用ProcessBuild

我有两个java进程。我想通过
ProcessBuilder
从另一个进程启动其中一个进程
我的问题是我只想开始另一个过程。我不想等待它的任何结果或输出。
即启动第二个流程后,两个流程将独立运行。
我该怎么做?在
ProcessBuilder
上不调用
waitFor
?通过不使用
输出流


注意:第二个过程不会产生输出,而是“永远”运行。实际上,这两个都是长时间运行的进程,根据文档,您是否使用process类的waitFor方法,如果是的话。那我想你不必这么做

取自


在单独的线程中使用ProcessBuilder运行进程,读取该线程中的stdout和stderr并忽略它们,在进程完成时让线程退出

使用线程。也就是说,在一个新线程中启动第二个进程。您必须使用输出,一些进程在读取输出缓冲区之前不会退出,但是您可以简单地读取流并忽略它的结果。但基本答案是使用
线程
如果第二个进程不产生任何输出并永远运行怎么办?如果第二个进程不产生任何输出并永远运行怎么办?那么整个应用程序将永远运行,除非您使用System.exit停止它,或者使用process.Destroy停止第二个进程。但我需要的是第一个和第二个进程第二个进程独立运行,第一个进程中没有线程,为第二个进程“绑定”/“保留”
**waitFor**

public abstract int waitFor()
                 throws InterruptedException

causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits.

Returns:
    the exit value of the process. By convention, 0 indicates normal termination. 
Throws:
    InterruptedException - if the current thread is interrupted by another thread while it is waiting, then the wait is ended and an InterruptedException is thrown.