waitFor表示进程已结束,但是您不能确定从stdout和stderr收集字符串的线程也完成了,因此使用闩锁是朝着正确方向迈出的一步,但不是最佳选择。您不必等待闩锁,而可以直接等待线程:
Thread stdoutThread = new Thread(new StreamConsumer(procOut, output)).start();Thread stderrThread = ......int ret = exec.waitFor();stdoutThread.join();stderrThread.join();
顺便说一句,将行存储在
StringBuffers中是无用的工作。请
ArrayList<String>改用,不做任何转换就将行放置在那里,最后以循环的方式检索它们。



