Java includes an elegant interprocess communication mechanism via the wait( ), notify( ), and notifyAll( ) methods.
These methods are implemented as final methods in Object, so all classes have them.
wait() method on an object, it voluntarily releases the lock it holds on that object and enters the “waiting” state. It will remain in this state until another thread notifies it (using notify() or notifyAll()) on the same object.wait() can only be called from within a synchronized block or method that holds the lock on the object. Otherwise, it will throw an IllegalMonitorStateException.notify() method on an object, it wakes up a single thread that is waiting on that same object. If multiple threads are waiting, the JVM doesn’t guarantee which one will be awakened.wait(), notify() must also be called from within a synchronized block or method that holds the lock on the object.notifyAll() method wakes up all the threads that are waiting on the same object. Each of the awakened threads will then compete to re-acquire the lock on the object.wait() and notify(), notifyAll() must be called from within a synchronized block or method that holds the lock on the object.
package interthreadedcommunication;
class Banking {
static public int balance = 0;
//Method to withdraw amount
public synchronized void withdraw(int amount){
if(balance <= 0){
try {
System.out.println("Waiting for your balance to update "
+ "before withdrawal of $"+amount);
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
balance = balance - amount;
System.out.println("Withdrawal successful. "
+ "The current balance is: $"+balance);
}
//Method to deposit amount
public synchronized void deposit(int amount){
System.out.println("We are depositing the amount $"+amount);
balance = balance + amount;
notify(); //Notify thread after depositing money
}
}
//Creating a thread to withdraw money
class BankThread1 extends Thread {
Banking banking;
public BankThread1(Banking banking) {
this.banking = banking;
}
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
banking.withdraw(1500);
}
}
//Creating a thread to deposit money
class BankThread2 extends Thread {
Banking banking;
public BankThread2(Banking banking) {
this.banking = banking;
}
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
banking.deposit(5000);
}
}
public class WaitNotifyExample {
public static void main(String[] args) {
Banking banking = new Banking();
BankThread1 bThread1 = new BankThread1(banking);
BankThread2 bThread2 = new BankThread2(banking);
//Start both the thread
bThread1.start();
bThread2.start();
}
}
Output
Waiting for your balance to update before withdrawal of $1500
We are depositing the amount $5000
Withdrawal successful. The current balance is: $3500
wait(), notify(), and notifyAll() must be called within a synchronized block or method on the object being waited on/notified.wait() to pause their execution and release the lock.notify() or notifyAll() to signal waiting threads that a condition might have changed.wait() within a while loop that checks the condition. This is to handle “spurious wakeups” (where a thread might be woken up without a notify() call) and to re-verify the condition after re-acquiring the lock.Inter-threaded communication is a fundamental concept in concurrent programming, allowing you to build more complex and efficient applications by coordinating the actions of multiple threads. Understanding and correctly using wait(), notify(), and notifyAll() is crucial for achieving this in Java.
You must be logged in to submit a review.