This code is giving IllegelMonitorStateException . Because I am calling wait() mthod from not synchronized context . But I am not able to understand that what will happen to add synchronized in this ? why we need synchronized ? please can any body explain me ...
suppose that this code is accepted by java, then there will exist a potential race condition. Consider following flow : 1) thread1 enters method1, test for "YourTestCondition" decides that it should call wait. (NOTE that it has just decided, it has not called wait()) 2) thread2 enters method2, does the processing, checks if "YourTestCondition" is set to true, if so calls notify. This goes unheard as thread1 is not yet waiting 3) thread1 calls wait()
How can this be solved ? By putting the wait and notify calls inside synchronized blocks.. On the side notes, wait() call releases the object lock prior to waiting and reaquires the object lock after returning from the wait()method.
If you look at the API details looking specifically at the details for the wait() method in Object (can't include direct URL to a method apparently), it says:
This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor. ... IllegalMonitorStateException - if the current thread is not the owner of the object's monitor.
That points to the notify method where the details include:
This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:
By executing a synchronized instance method of that object. By executing the body of a synchronized statement that synchronizes on the object. For objects of type Class, by executing a synchronized static method of that class.
Let us say that there is a thread called A.This thread enters the wait(). So once it enters the wait() method the lock is released. Now how this thread A gets notified.Is it the same thread that invoke the notified method???
Or the notification to thread A is done by some other thread.
Let us say that there is a thread called A.This thread enters the wait(). So once it enters the wait() method the lock is released. Now how this thread A gets notified.Is it the same thread that invoke the notified method??? Or the notification to thread A is done by some other thread.
--->Ok now following u, as u say there is a thread called A. This thread enters the wait(). Now here i guess u missed out something, wait() method on which object?. There has to be a object on which the thread has to be kept waiting. -->So, now the thread invokes wait() on the object. This releases the lock held by the thread for that object and this thread waits until another thread invokes the notify() method or the notifyAll() method for this object. U getting my point? Some other thread is having the lock on the object. --->Now, when the other thread invokes notify on the object, according to API:
Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation.... The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object;