my dog learned polymorphism*
The moose likes Beginning Java 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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "InterruptedException" Watch "InterruptedException" New topic
Author

InterruptedException

Mauro Trevigno
Ranch Hand

Joined: Aug 27, 2012
Posts: 40

Hi guys,



// Im using Eclipse Juno, and I receive Unhandled exception type InterruptedException if I dont throw InterruptedException, I found some similar problem and everybody get IllegalMonitorStateException.

I check the Api and found this. InterruptedException Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, ***** so i think notify() interrupt the wait() status.** im ok??

And about IllegalmonitorState - Thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor. How I know If im owning a specified monitor(is oo object)??

Which one applies to this case?

Sorry for so many questions.

Thanks!

Regards,
-M

[Edit]
Mauro, I edited your post to shorten the comment. Having long code lines like that can really interrupt the flow and format of the page and makes it hard to read. -steve-
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Mauro Trevigno wrote:
// Im using Eclipse Juno, and I receive Unhandled exception type InterruptedException if I dont throw InterruptedException,


Correct, you need to handle InterruptedException. If you don't understand this then you should brush yourself up on error/exception handling in Java. This Tutorial is a good place to start.



I found some similar problem and everybody get IllegalMonitorStateException.


The IllegalMonitorStateException occurs because you are trying to call wait() and notify() on an Object even though you don't hold that Object's lock. You gain the Object's lock by synchronizing on the object. In your example code you synchronize on the current Thread Object, which is not the right thing to do. Instead synchronize on oo.


I check the Api and found this. InterruptedException Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, ***** so i think notify() interrupt the wait() status.** im ok??

No. The notify() method does not interrupt the wait(), it properly signals that it is ok to continue. Plus, in your posted code, the notify() will never get called. The oo.wait() would wait for a different thread to call notify() so it can continue. You can't notify() from the same thread you wait() in because the wait() prevents the thread from executing.

And about IllegalmonitorState - Thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor. How I know If im owning a specified monitor(is oo object)??

You synchronize on oo, not on the current thread.


Which one applies to this case?

Both, but for different reasons.

Steve
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40

Steve Luke wrote:
And about IllegalmonitorState - Thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor. How I know If im owning a specified monitor(is oo object)??

You synchronize on oo, not on the current thread.


Unfortunately, while it will prevent the exception, it is not very useful -- the oo reference is pointing to an object that is only accessible within the method, hence, synchronizing (or using notifications) on it makes little sense.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mauro Trevigno
Ranch Hand

Joined: Aug 27, 2012
Posts: 40

Thank You!

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: InterruptedException
 
Similar Threads
INTERRUPTEDEXCEPTION
Compile time vs Runtime errors
IllegalMonitorStateException
Thread synchronization doubt