The Java Program: Update.java

  1 class Account {
  2    private int total = 0;
  3 
  4    public synchronized void deposit (int i) { total += i; }
  5                                                  
  6    public synchronized boolean withdraw (int i) {
  7       if (i <= total) {
  8          total -= i;
  9          return true;
 10       }
 11       return false;
 12    }
 13 
 14    public int balance () { return total; }
 15 }
 16 
 17 class Transaction extends Thread {
 18    private static Account accounts [] = new Account [100];
 19    static {
 20       for (int i=0;i<accounts.length;i++) {accounts[i]=new Account();}
 21    }
 22 
 23    private Account a;
 24    Transaction (int no) { a = accounts[no]; }
 25 
 26    public void run () {
 27       // get amount, etc
 28       int d = 100;
 29       // withdraw amount (if possible)
 30       if (a.withdraw(d)) {
 31       }
 32       System.out.println (a.balance());
 33       // dispense receipt, etc
 34    }
 35 }
 36 
 37 class Update {
 38    public static void main (String args[]) {
 39       new Transaction (3).start();
 40       new Transaction (3).start();
 41    }
 42 }