• Post Reply Bookmark Topic Watch Topic
  • New Topic

IllegalMonitorStateException

 
tanu dua
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to "Threads and Synchronization."
 
tanu dua
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry Ernest...

From the next time it ll not happen...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!