WaitvsNotify
WaitvsNotify
package com.example.demo.threads;
class BankAccount {
private int balance = 1000; // Shared resource
balance += amount;
System.out.println(Thread.currentThread().getName() + " completed deposit.
New balance: " + balance);
How It Works
1. Scenario:
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.