2013-04-08 52 views
0

我有2個java進程。我想通過ProcessBuilder從另一個流程啓動其中一個流程。
我的問題是我只想啓動其他進程。我不想等待任何結果或輸出。
I.e.在開始第二個過程之後,這兩個過程將獨立運行。
我該怎麼做?不要致電ProcessBuilder上的waitFor?通過不消耗OutputStreams產生另一個進程而不是等待它

注意:第二個過程不產生輸出並且「永遠」運行。其實兩者都是長時間運行的過程

+1

使用線程。也就是說,在新線程中啓動第二個進程。 – 2013-04-08 07:17:47

+0

你必須消耗輸出,一些進程不會退出,直到讀取輸出緩衝區,但你可以簡單地讀取流並忽略它的結果。但基本的答案是使用'線程' – MadProgrammer 2013-04-08 07:18:18

+0

如果第二個進程不產生任何輸出並永遠運行會怎麼樣? – Jim 2013-04-08 07:20:32

回答

0

按照文檔,如果是的話,你使用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. 
0

拍攝於一個單獨的線程運行與您的ProcessBuilder過程中,讀取輸出和錯誤在該線程,並忽略他們,讓線程退出時,該過程後

+0

如果第二個過程不產生任何輸出並永遠運行,該怎麼辦? – Jim 2013-04-08 07:22:56

+0

然後整個應用程序將永遠運行,除非你用System.exit停止它,或者你用Process.destroy停止第二個進程 – 2013-04-08 07:45:09

+0

但是我需要的是第一個和第二個進程獨立運行**而沒有線程第一個過程爲第二個過程「綁定」/「保留」 – Jim 2013-04-08 08:39:29

相關問題