Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

what happens when notified during InterruptedException?  RSS feed

 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a thread is interrupted while wait()ing on an object, it competes for the object's monitor before the InterruptedException is thrown.

What happens if notifyAll() is called on the object after the interruption, but before the thread regains the monitor?

My guess is that the InterruptedException is still thrown, rather than the wait() method returning as it would on a normal notification. If true, though, that makes it easy to miss a notification under these circumstances.

Anyone know if my guess is correct?
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A notification that is send when there are no threads waiting is just discarded... this is one of the reasons why you can't depend on it directly. You should always...

1. Check the completion condition prior to a wait() method call.

2. Set the completion condition prior to notify() method call. And this set condition must be done in the same synchronized block as the notify() call to prevent another race condition.

3. And of course, you should never assume that the completion condition is set upon return from a wait() method call.

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!