Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What causes InterruptedException? How to recover?

 
Simon Cockayne
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Calling wait() on an object can throw InterruptedException.

The API states:

"if another thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown."

1) Is this interrupt ONLY caused by the interrupt() method?

2) How could another thread call interrupt() BEFORE the current thread was waiting for a notification!

3) Why would you want to call interrupt() on a thread?


Cheers,

Simon.
[ October 04, 2006: Message edited by: Simon Cockayne ]
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Simon,

1.If a thread is waiting (sleep, wait, join, IO operation, etc) , it can be interrupted using the interrupt method because an Exception raises - this is the brutal way to "wake up" a thread. But if the thread is now waiting then only the interrupted state is set.

2.I also don't know what is really mean with "before waiting ...."? This can bequestion for the "Threads and Synchronization" forum, I think I'll make a try.

3.You may be not but, by accident or purpose somebody can knock down a the application just by interrupt a client thread.

Regards M.
 
Simon Cockayne
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mihai,

Thanks for the feedback.

I did some testing in a few code examples...

Ergo...I don't agree with your statement "brutal way to wake up a thread".

If a thread A is waiting (for the lock on an object)...and another thread B has the monitor (lock) on the object thread A is waiting on...then thread A will not be "woken up" by a thread calling A.interrupt();

Only when the thread A does get the chance to obtain the lock on the object THEN...will it wake up and the InterruptedException will be thrown.

For my part I'd like to do some more research on how to protect a thread against being interrupted.


What do you say?

Si.
 
Simon Cockayne
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mihai,

I did some testing, see the code below:

*Seemingly* the thread that waits (a1) is not woken up by the interrupt...


<pre>
// Only if I remove the comments around the next section...(which indirectly causes bThread to leave the synchronized block..thus giving up up its lock on obj1)...does aThread *actually* wake up and throw the interrupted exception)
</pre>


<b>Output (Without bThread being stopped):</b>

06-Oct-2006 11:35:46 ThreadTest2 main
INFO: GO!
06-Oct-2006 11:35:46 ThreadTest2 main
SEVERE: Pause 5000 before interrupt
06-Oct-2006 11:35:46 ThreadTest2$aThread run
INFO: aThread 'Thread-1' running
06-Oct-2006 11:35:46 ThreadTest2$aThread run
INFO: aThread holds lock='false'
06-Oct-2006 11:35:46 ThreadTest2$aThread run
INFO: aThread 'Thread-1has lock on ThreadTest.obj1
06-Oct-2006 11:35:46 ThreadTest2$aThread run
INFO: aThread 'Thread-1will wait...
06-Oct-2006 11:35:46 ThreadTest2$bThread run
INFO: bThread 'Thread-2' running
06-Oct-2006 11:35:46 ThreadTest2$bThread run
INFO: bThread 'Thread-2Got lock on obj1
06-Oct-2006 11:35:51 ThreadTest2 main
INFO: Interrupt aThread...
06-Oct-2006 11:35:51 ThreadTest2 main
INFO: ...Interrupt aThread

<b>Output (bThread being stopped...by removing comments in code as directed):</b>

06-Oct-2006 11:37:24 ThreadTest2 main
INFO: GO!
06-Oct-2006 11:37:24 ThreadTest2 main
SEVERE: Pause 5000 before interrupt
06-Oct-2006 11:37:24 ThreadTest2$aThread run
INFO: aThread 'Thread-1' running
06-Oct-2006 11:37:24 ThreadTest2$aThread run
INFO: aThread holds lock='false'
06-Oct-2006 11:37:24 ThreadTest2$aThread run
INFO: aThread 'Thread-1has lock on ThreadTest.obj1
06-Oct-2006 11:37:24 ThreadTest2$aThread run
INFO: aThread 'Thread-1will wait...
06-Oct-2006 11:37:24 ThreadTest2$bThread run
INFO: bThread 'Thread-2' running
06-Oct-2006 11:37:24 ThreadTest2$bThread run
INFO: bThread 'Thread-2Got lock on obj1
06-Oct-2006 11:37:29 ThreadTest2 main
INFO: Interrupt aThread...
06-Oct-2006 11:37:29 ThreadTest2 main
INFO: ...Interrupt aThread
06-Oct-2006 11:37:29 ThreadTest2 main
SEVERE: Pause 5000 before stop_bThread
06-Oct-2006 11:37:34 ThreadTest2 main
SEVERE: stop_bThread = true;
06-Oct-2006 11:37:34 ThreadTest2$bThread run
INFO: bThread 'Thread-2Lost lock on obj1!!!
06-Oct-2006 11:37:34 ThreadTest2$bThread run
INFO: bThread 'Thread-2' about to end
06-Oct-2006 11:37:34 ThreadTest2$aThread run
SEVERE: aThread 'Thread-1interrupted!!!
06-Oct-2006 11:37:34 ThreadTest2$aThread run
INFO: aThread holds lock='true'



[ October 06, 2006: Message edited by: Simon Cockayne ]

[ October 06, 2006: Message edited by: Simon Cockayne ]
[ October 06, 2006: Message edited by: Simon Cockayne ]
 
Jean S´┐Żbastien
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Simon,

Regarding your questions:

1. Yes.

2. If different threads are running but they are not synchronized on the same object then they don't have to wait for each other. The OS distributes the processing time among the threads and each thread will get some processing time. When a thread has got the processing time it can interrupt other threads if it has a reference to them. So if a thread is executing its run method, other threads can also execute their run method "at the same time". If the threads are synchronized on the same object then they will be executed one after the other.

3. Good question. I'll try to give an answer. This is useful when you want to wake up a thread only when a specific condition is met. Suppose you have a multi-threaded application synchronizing on an Integer. That Integer is a counter, it is incremented from time to time. Let's say that you have a special thread that must do something when the Integer (starting at 0) as reached 100000 and doesn't need to do anything the rest of the time. If you let that special thread waits and then notify it every time the Integer is changed to check if the Integer as reached 100000 you'll be wasting a lot of processing time. Better would be to only wake up that special thread when the Integer as reached 100000. This can be done by never using the notify or notifyAll method on the Integer (otherwise the special thread could wake up) and by calling the interrupt method of the special thread when the Integer as reached 100000.


I would be interested in other opinions regarding question 3...


Regarding your code:

If you call interrupt() the thread you have interrupted will try to acquire the lock on the object it is synchronizing on. The exception will be thrown when the thread acquires the lock (the try-catch block is within a synchronized block and will only be executed if the thread has the lock). If the object the thread is synchronizing on is not released the exception will not be thrown (because the thread cannot acquires the lock).


Greetings,

Jean
[ October 07, 2006: Message edited by: Jean S´┐Żbastien ]
 
ken bonzo
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

do I have to recover the InterruptedException? I do not call the interrupt method at all

Regards
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic