process.waitFor and readLine() blocks, help please!

G

googlepost

Hi,

I would like to know why the process.waitFor() call here, hangs
sometimes, not all the time! In fact, after looking at more carefully,
the reader.readLine() call blocks.

The process.waitFor() call alone is done in a separate thread, because
the thread that calls the execute method is not intended to wait until
the process is done. It will wait only for a timeout amount of
seconds.

The error stream and output stream are read in separate thread as
well.

Any help soon would be greatly appreciated. The
getCompleteCommandLine(), getCompleteEnvVars(), getworkDir() are
not listed her for convenience.

googlepost.

---------------------------------------------------------------------------

import OutputReader;
public class test
{

public int execute(ManagedObject mo, int id) throws
ExecutionException
{
ProcessData procData = null;

OutputReader stdoutReader = null;
OutputReader stderrReader = null;

int exitVal = -Integer.MAX_VALUE;
java.lang.Process process = null;

try
{
process = Runtime.getRuntime().exec(getCompleteCommandLine(),
getCompleteEnvVars(),
getworkDir());

OutputStream os = process.getOutputStream();

stdoutReader = new OutputReader( process.getInputStream(),
"stdout", getName());
stderrReader = new OutputReader( process.getErrorStream(),
"stderr", getName());

os.flush();

ProcessWaiter procWaiter = new ProcessWaiter( process );
procWaiter.start();

// check for timeout
long time = 0;

// times out after 90 secs.
int timeout = 90;
procWaiter.join(1000*timeout);

if (procWaiter.isAlive())
{
// timed out..
procWaiter.timeOut();
}
else
{
try
{
exitVal = process.exitValue();
}
catch (IllegalThreadStateException e)
{
// should never happen !! not alive !!

}

if (exitVal == 0)
{
return 0;
}
else
{
// get the error stream.
System.out.println("error");
return -l;

}
}
return result;
}
catch (Exception e)
{
e.printStackTrace();
}
}


class ProcessWaiter extends Thread
{
java.lang.Process _process = null;
boolean _timedOut = false;

public ProcessWaiter( java.lang.Process process )
{
_process = process;
}

public synchronized void timeOut()
{
_timedOut = true;
interrupt();
}


public void run()
{
try
{

try
{

_process.waitFor(); //***** HANGS
SOMETIMES!!!!!!***//
}
// catch (InterruptedException e)
catch (Exception e)
{
System.out.println("interrupted");
_process.destroy();
}
}
}
catch (Exception e)
{
System.out.println("error");
}
}
}
}

========================================================
Outputreader class:
-------------------

import org.apache.log4j.Logger;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;


public class OutputReader extends Thread
{
InputStream _stream = null;
String _streamType = null;
String _procName = null;

public OutputReader( InputStream inputStream, String streamType,
String procName )
{
super();
_stream = inputStream;
_streamType = streamType;
_procName = procName;
this.start();
}

public synchronized void run()
{
try
{
String inStr = null;
String resultStream = null;

InputStreamReader streamReader = new
InputStreamReader(_stream);
BufferedReader reader = new BufferedReader( streamReader );
String nextLine;
while ((nextLine = reader.readLine()) != null) //**THIS
BLOCKS**//
{

// Don't do anything with it for now!!!!
nextLine = _streamType + ": " + nextLine;
if (_streamType.equals("stdout"))
{
System.out.println(_procName + ": " + _streamType + ":
" + nextLine);
}
else
{
System.out.println(_procName + ": " + _streamType + ":
" + nextLine);
}
}
reader.close();
streamReader.close();
}
catch (IOException e)
{
System.out.println("IO Exception streaming output from
Process");
}
}
}
Post a follow-up to this message

Message 2 in thread
From: googlepost ([email protected])
Subject: Re: process.waitFor() hangs sometimes, help please!


View this article only
Newsgroups: comp.lang.java.developer
Date: 2003-07-20 23:14:17 PST


In fact, more specifically, when you read the inputstream, it blocks
at

while ((nextLine = reader.readLine()) != null) //readLine() blocks!!

in the outputreader class. What could we do about this? Any help would
be greatly appreciated.

googlepost.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top