Showing posts with label thread notifyAll. Show all posts
Showing posts with label thread notifyAll. Show all posts

Sunday, 2 March 2014

Why Wait and Notify is in Object Class

In java, we are acquiring locks on the resources. Every Object treated as a resource in java. Since Java is multi threaded, there is a possibility for resource corruption, if multiple threads are working on the same resource. There should be a mechanism, to safely update the resource. Here the monitor concept came to picture.

While performing any update on the resource, a thread must acquire a lock on the resource. Then the thread which acquires lock on the resource, update the resource, remaining all threads which want to work on this resource must wait until it finishes. Once the the thread finishes its updation with the resource, it asks the resource, to notify remaining threads to update. Same procedure like acquiring lock, releasing lock, notifying other threads repeats.

Since in java every resource considers to be an object, so that wait, notify and notifyAll kept in Object class.


Thread States                                                 Dead Lock                                                 Home

Thread notify

public final void notify()
Wakes up a single thread that is waiting on this object's monitor. Suppose more than one thread is waiting on this object's monitor, then only one thread gets notified, the way the thread chosen is totally implementation dependent.

public final void notifyAll()
Wakes up all threads that are waiting on this object's monitor. A thread waits on an object's monitor by calling one of the wait methods. Now all the awakened threads also in competition for the resource.

Thread wait: release resources                                                 Thread states                                                 Home

Thread wait

wait method causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

Object class has 3 overloaded variations of wait method.

public final void wait() throws InterruptedException
Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. To call the wait(), current thread must get Objects monitor, other wise IllegalMonitorStateException thrown.

When wait called on particular object, The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method.

public final void wait(long timeout) throws InterruptedException
Causes current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.

public final void wait(long timeout, int nanos)throws InterruptedException

Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.



Producer Consumer Problem                                                 Notify thread                                                 Home