| 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
|
|
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
|
|
|
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...
|
 |
 |
|
|
subject: IllegalMonitorStateException
|
|
|