"waiting and notifying are means of communication between threads that synch on the same object.These methods can only be executed on an object whose lock the thread holds, otherwise it will result in an IllegalMonitorStateException"
Can some one gimme an example so that i can understand this better?
MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
======================================================================= "waiting and notifying are means of communication between threads that synch on the same object. These methods can only be executed on an object whose lock the thread holds, otherwise it will result in an IllegalMonitorStateException" =======================================================================
Thr r two threads threadA & threadB, holding lock on the same object 'obj'. Assume at this instant threadA is excuting obj, as soon it is done/ or its time slice completes; It need to intimate threadB to PROCEED with obj, for this threadA calls notify() on obj, in turn obj wll intimate its threads to proceed with further execution.
A thread not concerned with an object (ie not holding a lock on it), cant command the object for intimation. If this happens an 'IllegalMonitorStateException' is thrown.
" i cnt call my friends(sm othr thread) gal(sm other object) to do good things (to perform sm action) bcoz the THREAD is with my friend(the othr thread). "
A Thread can call wait or notify on an object whose lock the thread has.
when does a Thread have an object lock? when it enters a synchronized block of code.
Therefore, an IllegalMonitorStateException is thrown when a Thread encounters a wait/notify method which is not in a synchronized block (i.e the Thread doesn't have the lock!)
Lets make it clearer What exactly does the wait method do? It commands the Thread executing that code to give up the lock on the object it is holding. How can a Thread give up the lock when it has none? Therefore an IllegalMonitorStateException is thrown. But if it were a synchronized block of code, the Thread executing that block has the object lock and therefore is capable of giving it up. And similar is the case with notify.
Imagine this: There is a circle of people and one baton. Only the person who holds the baton may speak, all the rest must wait for the person to pass the baton. I can say that " I'm giving up the baton" (by saying wait() ) only if I have the baton. If I don't, then the referee will say "you idiot, you don't have the baton and you can't speak, so shut up until you get the baton." But if I had it, I could say "ok, i'm done with this, who wants to speak next may take it" and that would be fine.
And Navneet, two objects cannnot hold the lock for the same object at the same time. That is the exact use of lock, only one thread may have it at one point of time, all the other threads that need to access that locked object will have to "wait" until the thread holding the lock relinquishes it.
[ August 27, 2005: Message edited by: Akshay Kiran ] [ August 27, 2005: Message edited by: Akshay Kiran ]
"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
Joined: Aug 18, 2005
By the way, Mike Corleone seems like you've popped out of Godfather- Mario Puzo and all the time I was thinking it sounded so familiar.