• Post Reply Bookmark Topic Watch Topic
  • New Topic

invalidMonitorState

 
mohit joshi
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can some one give a simple example when InvalidMonitorStateException is thrown? I got these exceptions long time back, but cant recall and didnt understand at that time why exactly i got these.
 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mohit ,
this exception is thrown when you try to call wait/notify/notifyAll any of these methods for an Object from a point in your program where u are not having a lock on that object.(i.e. u r not executing any synch. block/method of that object and still trying to call wait/notify/notifyAll)
here is a code first run it as it is. and the uncomment the lines
which are bold. again run it and u will get an exception.

regards
Deekasha
 
mohit joshi
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks deekasha, that was great example.
Another doubt which got clarified in the process, is that the compiler doesnt ensures that the wait() notify() methods are called from within a synchronized block.
 
Vladimir Kositsky
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure that notify and notifyAll throws any exception
 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vladimir,
notify and notifyAll both throw IllegalMonitorStateException. since This exception is a subclass of Runtime exception so u r not bound to catch it (although u may if u want to).
and being a runtimeexception this exception is not mentioned in the signature of notify,notifyAll methods.
regards
Deekasha
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!