IllegalThreadStateException and Thread.join

G

googlepost

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
 
S

Steve Horsley

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
 
R

Roedy Green

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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top