I have three files
1. Bank Instance
2. Bank
3. Main
// Bank Instance.java
public class BankInstance implements Runnable {
public Bank bank;
public Thread t;
public BankInstance(Bank bank,String nameOfCustomer){
this.bank = bank;
t = new Thread(this,nameOfCustomer);
t.start();
}
public void run() {
System.out.println("========================================================");
System.out.println("Initial amount in bank"+bank.displayAmount());
System.out.println("Amount after withdrawal\t"+bank.withdrawAmount(2000));
System.out.println("Amount after deposited "+bank.depositAmount(4000));
System.out.println("=========================================================");
}
}
// Bank.java
public class Bank {
public int mAmount;
public Bank(int initialAmount){
this.mAmount = initialAmount;
}
public synchronized int withdrawAmount(int amount){
this.mAmount = (amount < this.mAmount)? (this.mAmount -= amount):this.mAmount;
return this.mAmount;
}
public synchronized int depositAmount(int amount){
this.mAmount += amount;
return this.mAmount;
}
public synchronized int displayAmount(){
return this.mAmount;
}
}
// Main.java
public class Main{
public Main(){
System.out.println("\nWelcome to Bank \n");
}
public static void main(String[] args){
Bank bank = new Bank(20000);
BankInstance customer1 = new BankInstance(bank,"Kamal");
BankInstance customer2 = new BankInstance(bank,"Appu");
}
}
I want operations to be done in sequence, for example in **run method **
I want the output given below.
========================================================
Thread1 name is Kamal
call displayAmount();
call withdrawalAmount();
call depositAmount();
========================================================
========================================================
Thread2 name is Appu
call displayAmount();
call withdrawalAmount();
call depositAmount();
========================================================
But the output i got for the above program
========================================================
========================================================
Initial amount in bank20000
Initial amount in bank20000
Amount after withdrawal 18000
Amount after deposited 20000
=========================================================
Amount after withdrawal 16000
Amount after deposited 24000
=========================================================
Please help me to solve this issue . Thanks