Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > IllegalThreadStateException and Thread.join

Reply
Thread Tools

IllegalThreadStateException and Thread.join

 
 
googlepost
Guest
Posts: n/a
 
      07-22-2003
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
 
Reply With Quote
 
 
 
 
Steve Horsley
Guest
Posts: n/a
 
      07-22-2003
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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      07-22-2003
On 21 Jul 2003 17:49:56 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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.
 
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
if and and vs if and,and titi VHDL 4 03-11-2007 05:23 AM



Advertisments