Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > process.waitFor and readLine() blocks, help please!

Reply
Thread Tools

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

 
 
googlepost
Guest
Posts: n/a
 
      07-21-2003
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 ((E-Mail Removed))
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.


http://www.velocityreviews.com/forums/(E-Mail Removed) (googlepost) wrote in message news:<(E-Mail Removed). com>...
> Hi,
>
> I would like to know why the process.waitFor() call here, hangs
> sometimes, not all the time!
>
> 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)
> {
>
> // 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");
> }
> }
> }

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Help Help Pentax S5i Help needed (Please) The Martian Digital Photography 14 06-20-2008 07:56 AM
HELP - HELP - HELP =?Utf-8?B?S2ltb24gSWZhbnRpZGlz?= ASP .Net 4 03-09-2006 12:46 PM
HELP WANTED HELP WANTED HELP WANTED Harvey ASP .Net 1 07-16-2004 01:12 PM
HELP WANTED HELP WANTED HELP WANTED Harvey ASP .Net 0 07-16-2004 10:00 AM
HELP! HELP! HELP! Opening Web Application Project Error =?Utf-8?B?dHJlbGxvdzQyMg==?= ASP .Net 0 02-20-2004 05:16 PM



Advertisments