I understand that withdraw method will be used by more than one thread so it's compulsory to use synchronize on it. Why do we have to synchronize the deposit method while it will be used by only one thread (addMoney thread)?
package com.codegym.task.task17.task1720;
public class ATM {
static BankAccount account = new BankAccount("Amigo");
public static volatile boolean isStopped;
public static void main(String[] args) throws InterruptedException {
addMoney.start();
SpendThread spendThread = new SpendThread();
SpendThread spendThread1 = new SpendThread();
SpendThread spendThread2 = new SpendThread();
spendThread.start();
spendThread1.start();
spendThread2.start();
Thread.sleep(4000);
isStopped = true;
}
private static Thread addMoney = new Thread() {
@Override
public void run() {
while (!isStopped) {
account.deposit("1000"); // Make a deposit
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
break;
}
}
}
};
public static class SpendThread extends Thread {
@Override
public void run() {
while (!isStopped) {
try {
account.withdraw("100"); // Make a withdrawal
} catch (NotEnoughMoneyException e) {
System.out.println("Insufficient money");
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
break;
}
}
}
}
}