• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to resolve IllegalMonitorStateException

 
Ranch Hand
Posts: 254
1
MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have created a class that extends Thread. I am running this by invoking the start() method on this object. I would also like to pause in this thread. By using wait() code throws an IllegalMonitorStateException.

How do I get rid of this exception? Pasted the code with the stack trace.



 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you read the Documentation for Object.wait() ? http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#wait()

It explains pretty clearly the criteria for using .wait() and also the scenario that will result in a IllegalMonitorStateException.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You must hold the lock on the Object you are using to wait() on. See the Object#wait() API and read the concurrency tutorial: http://docs.oracle.com/javase/tutorial/essential/concurrency/.

As a side note, wait() is usually used as part of inter-thread communication. Some other thread has to be able to wake this thread up using notify. Does some other thread have access to a reference to this thread? Using the Thread instance itself as the Object to wait() on is usually frowned upon - both for the previous reason and because you are more likely to get spurious wakeup signals with it. I would suggest you create a new Object whose specific purpose is to be used for the wait() / notify() (java.util.concurrent.locks.Lock and the related Condition work as a great alternatives with more 'expressive' interfaces.)
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your replies!
 
Ranch Hand
Posts: 172
Redhat Ruby C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if it is clear for you, but you just need to put "synchronized" into the method (or code blocks) to be the ownership of the monitor (by default it uses the "this" as the monitor object ). Like the following code:




The answer in stackoverflow might help you as well:

http://stackoverflow.com/questions/2779484/why-must-wait-always-be-in-synchronized-block
http://stackoverflow.com/questions/2862827/java-concurrency-synchronizedthis-and-this-wait-and-this-notify
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luan Cestari wrote:I don't know if it is clear for you, but you just need to put "synchronized" into the method (or code blocks) to be the ownership of the monitor (by default it uses the "this" as the monitor object ).


Although you are right in that is how you get ownership of the lock, just putting synchronized does not really let wait() work, it just prevents the exception. That is why Tim and I pointed the OP to documentation on how to use the method, rather than just telling him how to avoid an exception.
 
Luan Cestari
Ranch Hand
Posts: 172
Redhat Ruby C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Steve,

I think you are right as Ahsan thanked. Sorry, I posted before seen that reply and I was thinking that the Ahsan had a different question in mind (as he state "How do I get rid of this exception?" in the first post)

Regards
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic