This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes InterruptedException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "InterruptedException" Watch "InterruptedException" New topic
Author

InterruptedException

Sameer Amte
Ranch Hand

Joined: Oct 22, 2002
Posts: 38
Hi,
On start up of my Java Application, I create a Thread Pool and also initialise a proprietary API that makes socket connections intialises it's own Thread Pool etc.
I noticed that the Worker Threads in my Thread Pool were all interrupted by the JVM on start up. In the worker thread, I check if the task queue is empty and if it is empty the worker threads go into waiting state (i.e., I call wait()). My assumption has been that InterruptedException is an abnormal scenario and hence I was terminating my worker threads on receiving InterruptedException.
Does anyone know of other possible scenarios where an InterruptedException might be raised, apart from calling interrupt(), expiry of timeout of sleep/wait.
What are the best ways to handle InterruptedExceptions?

Thanks
Sameer
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Did you implement your own thread pool? Show us what the code looks like where you set it up. There may be something in there to explain the situation.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Sameer Amte
Ranch Hand

Joined: Oct 22, 2002
Posts: 38
Hi,
Below is the code for my Thread Pool class:


Here is the main class that initialises the Worker Threads

In the start() method above, putting a Thread.sleep(1000) bfore initialisation of the Thread Pool solved the problem of interrupted exception. If the sleep statement is removed then the problem is occurs again.

Thanks
Sameer
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
When you add an item to queue you notifyAll() that something has arrived. That will cause an InterruptedException in *all* of your worker threads.

The loop you have here is a common structure:

except that the exception is not fatal. If you ignore the exception we'll loop back to the if-empty test. If this is the first thread to be awakened the newly added item is in the queue and we exit the wait loop, get the item, etc. If another thread already pulled the queue item and the queue is empty, we wait again.

Imlementing your own is a great exercise, but if you prefer well-tested code this stuff is in JDK5, the ApacheCommons thread pool or the Doug Lea concurrent package.
Sameer Amte
Ranch Hand

Joined: Oct 22, 2002
Posts: 38
Hi Stan,
Thanks for the input. I have found the source of the InterruptedExceptions.
It is coming from some test code within my Application. Some of the proprietary API that we use was not terminating threads currently, and we had used Thread.activeCount and Thread.enumerate to get the list of running thread and try to interrupt them to check if this terminated them.
This was causing the InterruptedException. This is in line with my expectations as I was hoping that the JVM wont interrupt my working threads in some unknown way.

I am using the InterruptedException within my code to terminate my threads, so that if they get hung for some reason, I can call interrupt() and this will hopefully terminate them. Is this a valid use of the InterruptedException?

Thanks
Sameer
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think that queue.wait() is going to get an exception every time you do notifyAll() ... every time you put something new in the queue. Try it and see!
Sameer Amte
Ranch Hand

Joined: Oct 22, 2002
Posts: 38
Hi Stan,
I have tested it and does not give an exception when notifyAll is called. One of the waiting threads will just move from waiting to runnable state when notifyAll is called. I dont understand why you expect an exception to be raised.

Thanks
Sameer
Monmohan Singh
Ranch Hand

Joined: Aug 02, 2002
Posts: 82
thread.join throws an interrupted exception?
I can't think of a way to handle it. Now the reason a join is called is because i don't want the parent thread to start execution till the child has finished execution. so what do i do in case the join method throws exception
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
Don't know what the experts would say, but what about:



So done will be true if and only if no InterruptedException was thrown.
Olaf Kummer
Greenhorn

Joined: May 02, 2005
Posts: 11
Originally posted by Stan James:
When you add an item to queue you notifyAll() that something has arrived. That will cause an InterruptedException in *all* of your worker threads.


No. Object#notifyAll() will wake up all threads that might happen to be in an Object#wait(), but it will never send an InterruptedException and it will not get you out of a sleep call or the like..

Regards, Olaf
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Yup, I was wrong on that one. Sorry for any confusion caused.

I just spent the morning reading some wicked code that communicates between threads. The application thread puts a command where the worker thread can find it, calls notify() on the worker thread object, calls wait() on the command. The worker thread wakes up from a wait(), executes the command and calls notify() on it. The application thread wakes up from wait() and gets the results from the command. I haven't decided if it's slick or perverted.

I'm not sure yet who is waiting for the notify() in line 05.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

I haven't decided if it's slick or perverted.


I don't see the purpose for this. The only reason that I can think of, for doing this, is to make sure that all commands are executed in serial. This can be accomplished by declaring a global lock, synchronizing on it, and executing the command directly.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It looks overly complex to me, too. It executes commands serially on another thread and makes the runCommand method synchronous. I'm pretty sure this originated in sample code from a terminal emulator program. The commands do JNI calls to the emulator DLL.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: InterruptedException
 
Similar Threads
Thread seems to lock on IO
notifyAll and wait methods
Pattern for threaded logging
From Velmurugan's Notes
Last task not executed by thread from thread pool