Java exec method, how to handle streams correctly

后端 未结 2 1974
说谎
说谎 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 10:53

    waitFor signals that the process ended, but you cannot be sure the threads which collect strings from its stdout and stderr finished also, so using a latch is a step in the right direction, but not an optimal one. Instead of waiting for the latch, you can wait for the threads directly:

    Thread stdoutThread = new Thread(new StreamConsumer(procOut, output)).start();
    Thread stderrThread = ...
    ...
    int ret = exec.waitFor();
    stdoutThread.join();
    stderrThread.join();
    

    BTW, storing lines in StringBuffers is useless work. Use ArrayList<String> instead, put lines there without any conversion, and finally retrieve them in a loop.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题