• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread.wait(5000) throws interrupt?  RSS feed

 
Karen Boxby
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I'm missing something obvious here and can't figure it out. I looked at the couple of sample questions that Oracle provides for the Java SE 7 Programmer II exam Oracle Sample questions for Java SE7 Programmer II exam and one of them puzzles me. The code snippet from the exam question is shown below and my question is regarding the wait().
I understand that wait() can be interrupted by notify() or notifyAll() or by a spurious interrupt, but I didn't think it threw it's own InterruptedException when the timeout occurred. I compiled the code and it prints "1 thrown to main", which means it was indeed interrupted.

Is it the timeout that's generating the InterruptedException or is it something else? The Java documentation for Object's wait doesn't indicate that the timeout itself can throw the InterruptedException.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to print out the exception to find out what type of exception it actually is. It is not an InterruptedException, and it has nothing to do with the timeout. Like this:
or this:
 
Karen Boxby
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you! I should have thought of that - my apologies.

I did what you suggested and it's an IllegalMonitorStateException because wait() was not issued within a synchronized block.

Now it makes sense to me -- I'm so glad I asked and I so glad you helped out.

Thanks again.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!