This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes IllegalMonitorStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "IllegalMonitorStateException" Watch "IllegalMonitorStateException" New topic
Author

IllegalMonitorStateException

tanu dua
Ranch Hand

Joined: Apr 05, 2004
Posts: 145
Hi all !

Can u plz clear my following doubt about threads :

The wait() method throws IllegalMonitorStateException when the current thread
is not the owner of the objects' monitor (as specified by the documentation
of Java and also the Core Java Book).

Now my doubt is :

1) Thread becomes the owner of the objects monitor only through entering
into synchronized method of that object.

2) And wait can only be called in the synchronized method.

3) So,wait can only be called if the current thread is in the synchronized method
of the object and hence become the owner of the object monitor (as
from condition 1)

4) So, when the IllegalMonitorStateException can occur.

Thanx.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24054
    
  13

This will throw an IllegalMonitorStateException:


As you say, you're only allowed to call wait() if you own the monitor for the object. The Java compiler doesn't know this, however, and will happily compile code like the above where the caller clearly doesn't own the monitor.


[Jess in Action][AskingGoodQuestions]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24054
    
  13

Moving to "Threads and Synchronization."
tanu dua
Ranch Hand

Joined: Apr 05, 2004
Posts: 145
sorry Ernest...

From the next time it ll not happen...
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: IllegalMonitorStateException
 
Similar Threads
TwoThreads problem with exception
IllegalMonitorStateException
how to get lock of an object
synchronized keyword ?
Please explain how does this code reaches the Exception code and prints 20 GOTO 30