Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to resolve IllegalMonitorStateException

 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • 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.



 
Tim Cooke
Sheriff
Pie
Posts: 3124
134
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your replies!
 
Luan Cestari
Ranch Hand
Posts: 172
C++ Redhat Ruby
  • Mark post as helpful
  • send pies
  • 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: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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
C++ Redhat Ruby
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic