簡體   English   中英

產生另一個進程而不等待

[英]Spawn another process and not wait for it

我有2個Java進程。 我想通過ProcessBuilder從另一個進程中啟動一個進程。
我的問題是我只想開始其他過程。 我不想等待任何結果或輸出。
即,在啟動第二個進程后,這兩個進程將獨立運行。
我怎樣才能做到這一點? 通過不調用ProcessBuilder waitFor嗎? 是否不使用OutputStreams

注意:第二個進程不會產生輸出,而是“永遠”運行。 實際上兩者都是長期運行的過程

查看文檔,是否使用流程類的waitFor方法(如果是)。 然后,我想您不需要這樣做。

取自Process Java Doc

**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.

在單獨的線程中使用ProcessBuilder運行您的進程,在該線程中讀取stdout和stderr並忽略它們,讓線程在進程完成時退出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM