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

Discussion in 'Java' started by googlepost, Jul 21, 2003.

  1. googlepost

    googlepost Guest

    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 ()
    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.


    (googlepost) wrote in message news:<>...
    > 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");
    > }
    > }
    > }
     
    googlepost, Jul 21, 2003
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Zimmer

    Process waitFor command

    Matthew Zimmer, Jan 6, 2004, in forum: Java
    Replies:
    4
    Views:
    4,587
    Matthew Zimmer
    Jan 7, 2004
  2. Replies:
    6
    Views:
    4,740
  3. Kevin
    Replies:
    2
    Views:
    24,596
    Kevin
    Mar 31, 2005
  4. John Smith

    Process.waitFor() blocks forever

    John Smith, Oct 8, 2010, in forum: Java
    Replies:
    4
    Views:
    3,365
    Daniel Pitts
    Oct 12, 2010
  5. matt
    Replies:
    1
    Views:
    274
    George Ogata
    Aug 6, 2004
Loading...

Share This Page