aspose file tools*
The moose likes Threads and Synchronization and the fly likes How to resolve IllegalMonitorStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to resolve IllegalMonitorStateException" Watch "How to resolve IllegalMonitorStateException" New topic
Author

How to resolve IllegalMonitorStateException

Ahsan Bagwan
Ranch Hand

Joined: Oct 05, 2010
Posts: 158
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
Bartender

Joined: Mar 28, 2008
Posts: 929
    
  44

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.


Tim Driven Development
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4174
    
  21

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.)


Steve
Ahsan Bagwan
Ranch Hand

Joined: Oct 05, 2010
Posts: 158
Thank you for your replies!
Luan Cestari
Ranch Hand

Joined: Feb 07, 2010
Posts: 162

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

Please, visit me for some cool tech post at www.ourdailycodes.com
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4174
    
  21

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

Joined: Feb 07, 2010
Posts: 162

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to resolve IllegalMonitorStateException