Java exec method, how to handle streams correctly

后端 未结 2 2001
说谎
说谎 2021-01-18 10:09

What is the proper way to produce and consume the streams (IO) of external process from Java? As far as I know, java end input streams (process output) should be consumed in

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-18 11:02

    Your appapproach is right, but is't better to remove CountDownLatch and use ThreadPool, and not create new Thread directly. From ThreadPool you will get two futures, which you can wait after to completion.

    But I'm not sure if I eventually need to synchronize with those consumer threads, or is it enough just to wait for process to exit with waitFor method, to be certain that all the process output is actually consumed? I.E is it possible, even if the process exits (closes it's output stream), there is still unread data in the java end of the stream?

    Yes, this situation may occurs. Termination and reading IO streams is unrelated processes.

提交回复
热议问题