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 is doing the stream output.
Thread t = new Thread(new Runnable(){
private boolean isProcessDone(Process p)
{
//not sure what to do here
}
public void run()
{
Process p = Runtime.getRuntime().exec("executable with output");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader error = new BufferedReader (new InputStreamReader(p.getErrorStream()));
while ( !isProcessDone(p) ) {
String line;
if( (line = input.readLine()) != null )
{
System.out.println(line);
}
if( (line = error.readLine()) != null )
{
System.out.println(line);
}
}
input.close();
error.close();
}
});
t.start();
My question is what belongs in the isProcessDone()
function. The example I am basing this off of uses the stream's ready()
function, but I am unclear whether this will work for programs that either std::err and std::out, but not both. I also tried using
try{
p.exitValue();
return true;
}catch(IllegalThreadStateException e){}
return false;
but then the thread finishes before the while loop has a chance to act on the streams, and the output is lost.