-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomer.java
More file actions
56 lines (50 loc) · 1.59 KB
/
Copy pathCustomer.java
File metadata and controls
56 lines (50 loc) · 1.59 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Session8_SynchronizedBlock;
/**
*
* @author lhhuong
*/
public class Customer {
private int balance = 1000;
public Customer(){
System.out.println("Tai khoan hien tai la: "+this.balance);
}
private synchronized void withdraw(int amount){
System.out.println("Dang thuc hien giao dich rut tien: "+ amount);
if(this.balance < amount){
System.out.println("So tien trong tai khoan khong du de thuc hien giao dich");
try {
wait();
} catch (InterruptedException e) {
System.out.println(e.toString());
}
}
this.balance -= amount;
System.out.println("GD rut tien thanh cong. Tai khoan hien tai la: "+this.balance);
}
private synchronized void deposit(int amount){
System.out.println("Dang thuc hien giao dich nap tien: "+amount);
this.balance += amount;
System.out.println("GD nap tien thanh cong. Tai khoan hien tai la: "+this.balance);
notify();
}
public static void main(String[] args){
Customer c = new Customer();
Thread t1 = new Thread(){
public void run(){
c.withdraw(2000);
}
};
t1.start();
Thread t2 = new Thread(){
public void run(){
c.deposit(5000);
}
};
t2.start();
}
}