wait(), notify(), notifyAll() in Java
When we come to multi-threading environment then we need of some communication between each thread like some thread has to perform task after other thread completes its task. In other way some thread has to communicate to other thread that you can takeover the Object monitor from waiting state. Basically we can say its a inter thread communication. For this we can use these methods very effectively and it does the same work for us.
All these methods are used in Java Multi-threading and used to communicate between each threads under same Object. Just by name itself we can assume that wait() makes the thread to wait or halt for some time, notify() and notifiAll() used to indicate other threads. Lets see each one by briefly and what is the use in multi-threading.
wait()
Makes current thread to wait and release the lock which acquired on the Object and waits for the other thread to call notify() or notifyAll() method. At the time of waiting other thread will does his process and communicate to waiting threading by calling notify or notifyAll method. One notify or notifyAll method called waiting thread will acquire the lock and it continues the process. There are 2 types of wait() methods in Java and they are
notify()
Once notify() called it makes waiting thread to acquire the Object and it starts the process. Suppose if more threads are waiting for the Object to acquire then any one thread will be acquire the Object monitor and the choice is arbitrary.
notifyAll()
Once notifyAll() method called it wake-up all threads that are waiting the Objects monitor. So the difference between notify() and notifyAll() is simple that once notify() method called only one thread will wake-up and by notifyAll() method all waiting threads will go to runnable state.
Next lets see simple example for using wait() and notify() with perfect example of amount deposit and withdraw from bank. Suppose when we withdrawing the amount if the balance is insufficient then the thread has to wait for deposit thread and then we need to withdraw the amount. For this we will be using wait() in withdraw thread and notify() in deposit thread.
OUTPUT:
All these methods are used in Java Multi-threading and used to communicate between each threads under same Object. Just by name itself we can assume that wait() makes the thread to wait or halt for some time, notify() and notifiAll() used to indicate other threads. Lets see each one by briefly and what is the use in multi-threading.
wait()
Makes current thread to wait and release the lock which acquired on the Object and waits for the other thread to call notify() or notifyAll() method. At the time of waiting other thread will does his process and communicate to waiting threading by calling notify or notifyAll method. One notify or notifyAll method called waiting thread will acquire the lock and it continues the process. There are 2 types of wait() methods in Java and they are
- wait() - Waits until other thread calls notify or notifyAll method. This method throws InterruptedException
- wait(long milliseconds) - Waits up-to given milliseconds and it starts the process. This method also throws InterruptedException.
notify()
Once notify() called it makes waiting thread to acquire the Object and it starts the process. Suppose if more threads are waiting for the Object to acquire then any one thread will be acquire the Object monitor and the choice is arbitrary.
notifyAll()
Once notifyAll() method called it wake-up all threads that are waiting the Objects monitor. So the difference between notify() and notifyAll() is simple that once notify() method called only one thread will wake-up and by notifyAll() method all waiting threads will go to runnable state.
Next lets see simple example for using wait() and notify() with perfect example of amount deposit and withdraw from bank. Suppose when we withdrawing the amount if the balance is insufficient then the thread has to wait for deposit thread and then we need to withdraw the amount. For this we will be using wait() in withdraw thread and notify() in deposit thread.
public class BankTrans implements Runnable {
private int amount = 0;
@Override
public synchronized void run() {
if(Thread.currentThread().getName().equals("withdraw")){
System.out.println("Entering to withdraw amount");
//Trying to withdraw 5000
if(this.amount >= 5000){
this.amount -= 5000;
}else{
try {
System.out.println("Insufficient balance and waiting for deposit....");
wait();
this.amount -= 5000;
System.out.println("Amount withdraw completed successfully :)");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}else{
//Depositing 10000 in user account;
System.out.println("Entering to deposit amount");
this.amount += 10000;
System.out.println("Amount deposited and notifying....");
notify();
}
}
public static void main(String[] args) {
BankTrans obj = new BankTrans();
Thread withDraw = new Thread(obj, "withdraw");
Thread deposit = new Thread(obj, "deposit");
withDraw.start();
deposit.start();
}
}
OUTPUT:
Entering to withdraw amount
Insufficient balance and waiting for deposit....
Entering to deposit amount
Amount deposited and notifying....
Amount withdraw completed successfully :)