Friday 27 January 2012

wait(), notify() and notifyAll() -Inter Thread Communications in java


Let us take a real-time scenario ,What is the use of  message alert in your mobile phone ?? .The answer is very simple ,once you get a message alert ,you can go and check your mail box and read the message.Suppose that your mobile is not providing message alert facility ,then what is the problem?.
You have to check your mailbox frequently to see a new message ,and it consumes a lot of time.This is nothing but ,polling in Computer Science.

Polling refers to the situation where one or more threads repeatedly checking one condition for performing some actions.Polling is usually implemented by a loop that is used to check some condition repeatedly.Once the condition is true, appropriate action is taken. This wastes CPU time.


 For example, consider the classic queuing problem, where one thread is producing some data and another is consuming it. To make the problem more interesting, suppose that the producer has to wait until the consumer is finished before it generates more data. In a polling system, the consumer would waste many CPU cycles while it waited for the producer to produce. Once the producer was finished, it would start polling, wasting more CPU cycles waiting for the consumer to finish, and so on. Clearly, this situation is undesirable.

To avoid polling, Java includes an elegant Inter-Thread communication mechanism via the wait( ),notify( ), and notifyAll( ) methods. These methods are implemented as final methods in Object, so all classes have them. All three methods can be called only from within a synchronized method. 

These methods are declared within Object, as shown here:
final void wait( ) throws InterruptedException
final void notify( )
final void notifyAll( )
Additional forms of wait( ) exist that allow you to specify a period of time to wait.

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

wait()

This method should only be called by a thread that is the owner of this object's monitor.wait( ) tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ) or notifyAll().
The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

wait(long timeout)

This method should only be called by a thread that is the owner of this object's monitor. 
This method causes the current thread (call it T) to place itself in the wait set for this object and then to give up the lock on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
  • Some other thread invokes the notify method for this object and thread T happens to be arbitrarily chosen as the thread to be awakened.
  • Some other thread invokes the notifyAll method for this object.
  • Some other thread interrupts thread T.
  • The specified amount of real time has elapsed, more or less. If timeout is zero, however, then real time is not taken into consideration and the thread simply waits until notified.
The thread T is then removed from the wait set for this object and re-enabled for thread scheduling. It then competes in the usual manner with other threads for the right to synchronize on the object.The thread T starts execution,once gets lock on the object.

If the current thread is interrupted by another thread while it is waiting, then an InterruptedException is thrown. 
Note that the wait method, as it places the current thread into the wait set for this object, unlocks only this object; any other objects on which the current thread may be synchronized remain locked while the thread waits.

wait(long timeout,int nanos)

This method is same as wait(long timeout) ,but it allows finer control over the amount of time to wait for a notification before giving up.You can specify timeout value more accurately using two parameters,
timeout - the maximum time to wait in milliseconds.
nanos - additional time, in nanoseconds range 0-999999.

notify( )
 

This method should only be called by a thread that is the owner of this object's monitor. 
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.

The awakened thread will not be able to proceed until the current thread releases the lock on this object. i.e. Once a thread calls notify() on an object,it won't immediately releases the lock on the object,lock will be released only after finishing the execution of the synchronized method or block. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.
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.
Only one thread at a time can own an object's monitor.

notifyAll()

Wakes up all threads that are waiting on this object's monitor.The highest priority thread will run first. A thread waits on an object's monitor by calling one of the wait methods.


The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.

This method should only be called by a thread that is the owner of this object's monitor.


 The links that may help you.
Inter-Thread Communication in Java with Example 

No comments:

Post a Comment