Threads communicate with one another to know their locking status, status of an event etc. Thread interacts with one another using Object class’s methods and they are:
- public final void wait() throws InterruptedException
- public final void notify()
- public final void notifyAll().
Note: wait(), notify() and notifyAll() must be called within a synchronized block, a thread cannot call wait() or notify() method on an object unless it acquires a lock on an object.
wait() Method
A thread is made to be waiting in a pool or waiting room until it gets notification from another thread. A thread calls wait() method to go and wait for some time. From this moment it will not execute any instruction until it gets notification. If many threads are waiting on the same object, only one thread will be allowed to proceed with its execution and it’s not guaranteed that the thread waiting for long will get a lock on that object. A thread can be interrupted when it was waiting, so use try/catch block around the wait() method to take care of exception.
1 2 3 4 5 |
try { wait(); } catch(InterruptedException ex) { //catch exception } |
The 2nd form of wait() method which takes number of millisecond as a parameter as a maximum time to wait. It will continue its execution whenever it gets notification or specified time has elapsed.
1 2 3 4 5 6 7 |
try{ synchronized(b){ b.wait(2000); //waits maximum specified time for only 2 sec } }catch(InterruptedException ex) { //catch exception } |
Note : If thread calls wait() and it does not owns the lock, it will throw an runtime IllegalMonitorStateException.
notify() and notifyAll() Methods
The notify() method sends a notification to a single thread which is waiting on the object’s lock. The notify() method wakes up only one thread which is waiting on object’s lock. If it is only single thread it will get object’s lock; if there are many threads which are waiting for object’s lock then it’s uncertain that which thread will get notification by notify() method.
The notifyAll() method notifies to all the threads which are waiting on a particular object’s lock. If there are not threads waiting for an object’s lock, notify() and notifyAll() has no effects.
When the wait() method is called on an object, the thread executing the code releases its lock on the object immediately. When notify() is called, thread does not releases its lock until it completes the synchronized block.
A simple example to calculate sum, using wait() and notify() method in synchronized block
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<strong>public</strong> <strong>class</strong> Addition <strong>extends</strong> Thread { <strong>int</strong> sum=0; <strong>public</strong> <strong>void</strong> run() { <strong>synchronized</strong> (<strong>this</strong>)//thread got lock { System.<em>out</em>.println("in synchronized block and calculating"); <strong>for</strong>(<strong>int</strong>i=0;i<=400;i++) { sum=sum+i; } notify(); //thread releases lock } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<strong>public</strong> <strong>class</strong> Test { <strong>public</strong> <strong>static</strong> <strong>void</strong> main(String [] args)<strong>throws</strong> InterruptedException { Addition b =<strong>new</strong> Addition(); b.start(); <strong>synchronized</strong>(b) //thread got lock { System.<em>out</em>.println("calling wait method"); b.wait(); System.<em>out</em>.println("Notification received"); } System.<em>out</em>.println(b.sum); } } |
output:
calling wait method
in synchronized block and calculating
Notification received
80200
Leave a Reply