-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample110.java
More file actions
31 lines (24 loc) · 800 Bytes
/
Example110.java
File metadata and controls
31 lines (24 loc) · 800 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Example 110 from page 83
//
class Bank {
private int account1 = 10, account2 = 20;
synchronized public void transfer(int amount) {
int new1 = account1 - amount;
Util.pause(10);
account1 = new1; account2 = account2 + amount;
System.out.println("Sum is " + (account1+account2));
} }
class Clerk extends Thread {
private Bank bank;
public Clerk(Bank bank) { this.bank = bank; }
@Override
public void run() {
for (;;) { // Forever
bank.transfer(Util.random(-10, 10)); // transfer money
Util.pause(200, 300); // then take a break
} } }
class Example110 {
public static void main(String[] args) {
Bank bank = new Bank();
new Clerk(bank).start(); new Clerk(bank).start();
} }