The Java Program: Consistent.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 synchronized (a) {
28 // get amount, etc
29 int d = 100;
30 // withdraw amount (if possible)
31 if (a.withdraw(d)) {
32 }
33 System.out.println (a.balance());
34 // dispense receipt, etc
35 }
36 }
37 }
38
39 class Consistent {
40 public static void main (String args[]) {
41 new Transaction (3).start();
42 new Transaction (3).start();
43 }
44 }