Using a thread to capture process output

前端 未结 4 489
深忆病人
深忆病人 2020-12-06 22:11

I am using a thread to capture stream output from a process, and then outputting that stream to the eclipse console. The question I have is when to terminate the thread that

4条回答
  •  伪装坚强ぢ
    2020-12-06 22:58

    You need to use Process.waitFor() to wait for process completion.

    Additionally, you need to consume stdout and stderr concurrently in order to avoid blocking and a possible process hang. Consequently you need two threads to read these streams, and to continue reading whilst the streams are available.

    See this Javaworld article for more info and a StreamGobbler implementation to consume the stdout/err.

提交回复
热议问题