• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

InterruptedException

 
Ranch Hand
Posts: 99
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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-
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Mauro Trevigno
Ranch Hand
Posts: 99
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You!

 
and POOF! You're gone! But look, this tiny ad is still here:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic