-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynchronization3.java
More file actions
40 lines (37 loc) · 1 KB
/
Copy pathSynchronization3.java
File metadata and controls
40 lines (37 loc) · 1 KB
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
30
31
32
33
34
35
36
37
38
39
40
class Customer {
int amount = 10000;
synchronized void withdraw(int amount) {
System.out.println("Going to withdraw");
if (this.amount < amount) {
System.out.println("Less balance.");
try {
wait();
} catch (Exception e) {
System.out.println(e);
}
}
this.amount -= amount;
System.out.println("withdraw completed");
}
synchronized void deposit(int amount) {
System.out.println("Going to deposit");
this.amount += amount;
System.out.println("deposit completed");
notify();
}
}
public class Synchronization3 {
public static void main(String[] args) {
final Customer c = new Customer();
new Thread(){
public void run() {
c.withdraw(14000);
}
}.start();
new Thread() {
public void run() {
c.deposit(200000);
}
}.start();
}
}