0

I've 2 Thread that work synchronized to withdraw money from the Bank.

I've created an Incharge Thread that should run every 30Seconds and its should take 15Seconds long.

During the 15 Seconds, Thread1 and Thread2 that withdraw the money should wait until Incharge Thread finish.

Which mean, T1 and T2 should have 15Seconds to withdraw the money until Incharge return from sleep until the balance is zero

I am trying to do it with t1.wait(15*SECOND) but I receive:

Exception in thread "inCharge" java.lang.IllegalMonitorStateException at java.base/java.lang.Object.wait(Native Method) at InCharge.run(InCharge.java:15) at java.base/java.lang.Thread.run(Thread.java:834)

My feeling is that I'm not doing it correct.

Main:

class Main {
    public static void main(String args[]) {
        int startingBalance = 100000;
        Bank bank = new Bank(startingBalance);
        Thread client1 = new ClientThread(bank, 1);
        Thread client2 = new ClientThread(bank, 2);
        Thread inCharge = new Thread(new InCharge(bank, client1, client2));
        client1.setName("T1");
        client2.setName("T2");
        inCharge.setName("inCharge");


        inCharge.start();
        client1.start();
        client2.start();
    }
}

InCharge :

public class InCharge extends Thread {
    public static final int SECOND = 1000;
    private Bank bank;
    private Thread t1,t2;
    InCharge(Bank bank, Thread t1, Thread t2){
        this.bank = bank;
        this.t1 = t1;
        this.t2 = t2;
    }
    @Override
    public void run() {
        while (true) {
            try {
                System.out.println("Charge is here!, current balance is: " + bank.getBalance());
                this.t1.wait(15*SECOND);
                this.t2.wait(15*SECOND);
                Thread.sleep(30 * SECOND);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

Bank:

public class Bank {
    private int balance;
    public Bank(int balance) {
        this.balance = balance;
    }
    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }
    public synchronized void withdraw(String name, int amount){
        if (this.balance - amount >= 0) {
            this.balance = this.balance - amount;
            System.out.println(name+" "+this.balance + " withdrawed - " + amount);
        }
    }
}

ClientThread:

public class ClientThread extends Thread {
    private Bank bank;
    private int client;

    ClientThread(Bank bank, int client) {
        this.bank = bank;
        this.client = client;
    }

    @Override
    public void run() {

        int randomNumber;
        while (bank.getBalance() > 0) {
            randomNumber = ((int) (Math.random() * (5000 - 1000 + 1)) + 1000);
            try {
                bank.withdraw(Thread.currentThread().getName(),randomNumber);
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
Itzik.B
  • 1,023
  • 2
  • 15
  • 35

0 Answers0