a Java application of mine is launching an external program using Runtime.exec(String[], String[], File). In general, this is working fine. However, I notice an important difference between Linux, and Windows. Suggest the following code snippet:
Process pr = Runtime.getRuntime(cmdArray, env, workDir);
startThreadProcessingStdout(pr.getInputStream());
startThreadProcessingStderr(pr.getErrorStream());
int status = pr.waitFor();
On Windows, my threads are done when waitFor() returns. (At least in the sense, that they have seen all required input.)
On Linux, however, the process is gone, but the threads are still busily reading input. I can fix that by changing my snippet as follows:
Process pr = Runtime.getRuntime(cmdArray, env, workDir);
AtomicBoolean outRunning = startThreadProcessingStdout(pr.getInputStream());
AtomicBoolean errRunning = startThreadProcessingStderr(pr.getErrorStream());
int status = pr.waitFor();
while (outRunning || errRunning) {
Thread.sleep(100);
}
However, that seems clumsy, and slow. Is there a better solution for my problem?