0% found this document useful (0 votes)
15 views

WaitvsNotify

The document presents an example of a BankAccount class demonstrating the use of wait(), notify(), and notifyAll() for thread coordination during deposits and withdrawals. It illustrates how threads handle insufficient balance scenarios, where a waiting thread is notified when a deposit occurs. Key points include the functionality of wait() for pausing threads and notifyAll() for waking them up, ensuring thread safety through synchronized methods.

Uploaded by

Vpn Account
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

WaitvsNotify

The document presents an example of a BankAccount class demonstrating the use of wait(), notify(), and notifyAll() for thread coordination during deposits and withdrawals. It illustrates how threads handle insufficient balance scenarios, where a waiting thread is notified when a deposit occurs. Key points include the functionality of wait() for pausing threads and notifyAll() for waking them up, ensuring thread safety through synchronized methods.

Uploaded by

Vpn Account
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Here's an example that demonstrates the use of wait(), notify(), and notifyAll() in the context

of a BankAccount class. In this example, threads coordinate to handle deposits and


withdrawals safely when the balance is insufficient.

package com.example.demo.threads;

class BankAccount {
private int balance = 1000; // Shared resource

// Synchronized method to deposit money


public synchronized void deposit(int amount) {
System.out.println(Thread.currentThread().getName() + " is trying to
deposit: " + amount);

balance += amount;
System.out.println(Thread.currentThread().getName() + " completed deposit.
New balance: " + balance);

// Notify waiting threads (withdrawal) that balance has been updated


notifyAll();
}

// Synchronized method to withdraw money


public synchronized void withdraw(int amount) throws InterruptedException {
System.out.println(Thread.currentThread().getName() + " is trying to
withdraw: " + amount);

// Wait until there is sufficient balance


while (balance < amount) {
System.out.println(Thread.currentThread().getName() + " is waiting for
sufficient balance...");
wait();
}

// Proceed with withdrawal


balance -= amount;
System.out.println(Thread.currentThread().getName() + " completed
withdrawal. New balance: " + balance);
}

// Method to get the current balance


public int getBalance() {
return balance;
}
}

public class WaitNotifyExample {


public static void main(String[] args) {
BankAccount account = new BankAccount(); // Shared resource

// Thread 1: Depositing money


Thread t1 = new Thread(() -> {
try {
Thread.sleep(2000); // Simulate delay
account.deposit(1000); // Deposit 1000
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "Depositor-1");

// Thread 2: Withdrawing money


Thread t2 = new Thread(() -> {
try {
account.withdraw(1500); // Withdraw 1500
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "Withdrawer-1");

// Thread 3: Withdrawing money


Thread t3 = new Thread(() -> {
try {
account.withdraw(500); // Withdraw 500
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "Withdrawer-2");

// Start all threads


t1.start();
t2.start();
t3.start();
}
}

How It Works

1. Scenario:

o Withdrawer-1 tries to withdraw 1500, but the initial balance is 1000.

o Since there’s insufficient balance, Withdrawer-1 goes into a waiting state


(wait()).

o Depositor-1 deposits 1000, increasing the balance to 2000, and calls


notifyAll() to wake up waiting threads.

o Withdrawer-1 and Withdrawer-2 recheck the balance and complete their


withdrawals in order.

2. Key Points:

o wait(): Makes a thread wait until the balance is sufficient. The thread releases
the lock and pauses.

o notifyAll(): Wakes up all threads waiting on the object (in this case,
BankAccount).
o Synchronized Block: Ensures thread safety by locking the shared resource
while modifying the balance.

You might also like