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

Recommended Answers

All 3 Replies

Although your individual Bank methods are synchronised, this only means that calls to each of those methods for the same Bank object will not overlap.
It looks like you want each execution of BankInstance's run method to complete before the next one starts. If so, you need to synchronise the run method, or the code inside it, using a single shared lock object.
(ps The Java API contains all kinds of useful classses for locks and synchronisation, with more being added in each release)

@JamesCherrill : Although your individual Bank methods are synchronised, this only means that calls to each of those methods for the same Bank object will not overlap.
Can you give me an example.

I have tried the above program using synchronized block statement and it worked . But i want to try synchronization to methods and is it possible ?

The synchronised keyword prevents two threads executing the synchronised code with the same lock. How you use it is up to you.
If I understand your implied requirement properly you have to synchronise the code in that run method

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.