I was trying wait and notify methods. Notify method when put in run method does not work but when the same method is put in some other method it works.
public class NewProgram {
static int balance = 0;
public void withdraw(int amount) {
synchronized(this)
{
if(balance== 0) {
try{
System.out.println("Waiting for deposit");
wait();
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
balance = balance - amount;
System.out.println("Amount Withdrawn Successfully");
}
public void deposit(int amount) {
balance = balance + amount;
System.out.println("Money Deposited Successfully");
// The Notify method here works
// synchronized(this)
// {
// notify();
// }}
public static void main(String[] args) {
NewProgram app = new NewProgram();
Thread thread1 = new Thread (new Runnable(){
@Override
public void run(){
app.withdraw(2000);
}
});
thread1.start();
Thread thread2 = new Thread(new Runnable(){
@Override
public void run() {
try {
Thread.sleep(5000);
// The Notify method here doesn't work
// synchronized(this) {
// notify();
// }
} catch (InterruptedException e) {
e.printStackTrace();
}
app.deposit(3000);
// The notify method from here doesn't work
//synchronized(this) {
// notify();
//}
System.out.println("Thread 2 ends");
}
});
thread2.start();
}}
So, here in this program withdraw method when balance is zero waits for deposit method for the money to be deposited. Then the lock on wait is removed and withdraw operations continue. This program runs perfectly when notify method is called in deposit method. But when notify is placed is run method of Thread 2 after calling the deposit method, the lock on wait is not removed, but the next statement Thread 2 ends gets executed and the program waits indefinitely. Please help me with this.