IllegalThreadStateException and Thread.join

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

  1. googlepost

    googlepost Guest

    Hi,

    I am trying to execute an '.exe' through Runtime.exec() and trying to
    read the exit value in a while loop.

    while (goodexitvalue || timeoutReached)
    {
    // Read i/p stream if available
    // Read error stream if available
    try
    {
    process.exitValue();
    }
    catch (IllegalThreadStateException e)
    {
    // the while continues until good value or timeout reached.
    }
    }

    Shutdown process.
    ================
    workerthread.join(10 secs) // HANGS !!!

    Meanwhile, a shutdown of the main agent happens, and the threadpool
    tries to shutdown its worker threads and does a join(10 secs), one of
    which is the one with the 'while loop'. This worker thread join hangs
    while we get a IIlegalThreadStateException() on the worker thread.

    Any help is appreciated.
    Thnx
    googlepost, Jul 22, 2003
    #1
    1. Advertising

  2. On Mon, 21 Jul 2003 17:49:56 -0700, googlepost wrote:

    > Hi,
    >
    > I am trying to execute an '.exe' through Runtime.exec() and trying to
    > read the exit value in a while loop.
    >
    > while (goodexitvalue || timeoutReached)
    > {
    > // Read i/p stream if available
    > // Read error stream if available
    > try
    > {
    > process.exitValue();


    Why do you call this method and ignore its return value?
    Maybe you misunderstand what the method does (or doesn't).

    > }
    > catch (IllegalThreadStateException e)
    > {
    > // the while continues until good value or timeout reached.
    > }
    > }
    >
    > Shutdown process.
    > ================
    > workerthread.join(10 secs) // HANGS !!!
    >
    > Meanwhile, a shutdown of the main agent happens, and the threadpool
    > tries to shutdown its worker threads and does a join(10 secs), one of
    > which is the one with the 'while loop'. This worker thread join hangs
    > while we get a IIlegalThreadStateException() on the worker thread.
    >


    The whole loop is a bad idea. The loop will spin throwing a continuous
    stream of IlegalThreadStateExceptions until the Process finishes.

    I suggest that you call Process.waitFor() and then get the exit value.
    This raises the requirement for a new thread to read the output streams
    from the process. But to prevent that thread spinning in an available()
    loop, I suggest that you use 2 threads and blocking reads. That is:

    * Spawn a Thread to read the getInputStream() stream
    * Spawn a Thread to read the getErrorStream() stream
    * Call Process.waitFor()
    * Get the Process exit value

    When the Process exits, the two threads will read EOF and should then
    close their streams.

    This approach works reliably for me.
    Steve
    Steve Horsley, Jul 22, 2003
    #2
    1. Advertising

  3. googlepost

    Roedy Green Guest

    On 21 Jul 2003 17:49:56 -0700, (googlepost)
    wrote or quoted :

    >workerthread.join(10 secs) // HANGS !!!


    The usual problem with join is you are going to sleep, waiting for
    your own death. You will never die while sleeping.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jul 22, 2003
    #3
    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. KittyCreation

    Thread and Join

    KittyCreation, Jul 30, 2005, in forum: Java
    Replies:
    4
    Views:
    418
    jan V
    Jul 30, 2005
  2. Alan Silver
    Replies:
    0
    Views:
    877
    Alan Silver
    Jun 5, 2006
  3. googleboy
    Replies:
    1
    Views:
    918
    Benji York
    Oct 1, 2005
  4. Lie Ryan
    Replies:
    2
    Views:
    411
  5. Rushi
    Replies:
    2
    Views:
    302
    7stud --
    Mar 20, 2009
Loading...

Share This Page