Bookmark Topic Watch Topic
  • New Topic

IllegalMonitorStateException  RSS feed

 
tanu dua
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Report post to moderator
It occurs when wait() is called outside a method or block synchronized on the object you're calling wait() on. In other words, your premise (2) is not true. The compiler does allow this because it cannot generally determine at compile time whether you own the lock at that point or not.

- Peter
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please don't post the same topic to multiple forums. It just wastes people's time. I'm closing this one; followups to the copy which I moved from "Java in General (Beginner)" to "Threads and Synchronization."
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!