• Post Reply Bookmark Topic Watch Topic
  • New Topic

notify() and InterruptedException

 
Harsh Pensi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When the notify() method is called on the 'jobs' object, Statements present in regions ONE , TWO and THREE get executed.
Questions :
- When is InterruptedException thrown in wait - notify usage?
- If it is thrown by wait, then why ONE is getting executed?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interrupted exception CAN be thrown from the wait() call if some other Thread calls Thread.interrupt() before or while the wait() call is blocking. If this occurred in your code I would expect the code to execute like:
- Code in block //two
- Code in block //three
- Code in block //one (wait begins again and could get notified)
- Code in block //three

However, it COULD be that some other step in you code also could throw an InterruptedException, and if this code is after the wait() call inside block //one then it would not be impossible to see
- Code in block //one (or at least some of it - up to the point where InterruptedException gets thrown from a different step)
- Code in block //two
- Code in block //three
 
Harsh Pensi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you Steve. But no code is apparently calling the Thread.interrupt() method.
I'm pasting a similar running code here.

Which gives the output :
Waiting for calculation...
Waiting for calculation...
Waiting for calculation...
ONE
TWO
THREE
ONE
TWO
THREE
ONE
TWO
THREE


Clearly ONE is not throwing InterruptedException, then why do ONE and TWO both get printed?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because TWO is NOT printed in your catch blcok. You have an EMPTY catch block:
 
Harsh Pensi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow..that was quick..Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!