-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountTest.java
More file actions
66 lines (52 loc) · 1.55 KB
/
Copy pathAccountTest.java
File metadata and controls
66 lines (52 loc) · 1.55 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
57
58
59
60
61
62
63
64
65
66
package ThreadTest.exers;
/* 两个储户分别向一个账户存3000, 分三次, 每次1000 ,每次存完打印余额 */
import java.util.concurrent.locks.ReentrantLock;
public class AccountTest {
public static void main(String[] args) {
Customer customer = new Customer();
Thread a1 = new Thread(customer);
Thread a2 = new Thread(customer);
a1.setName("储户A");
a2.setName("储户B");
a1.start();
a2.start();
}
}
class Account{
private double balance;
public Account(double balance) {
this.balance = balance;
}
public void init (){
this.balance = 0;
}
public void deposit(double newB){
this.balance += newB;
}
public double getBalance(){
return this.balance;
}
}
class Customer implements Runnable{
Account acc = new Account(333);
private ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
try {
lock.lock();
for(int i = 0; i < 3; i++){
acc.deposit(1000);
// this.notify(); notify and wait can not be used in lock method
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 存了 1000" );
System.out.println("账户余额: " + acc.getBalance());
}
}finally {
lock.unlock();
}
}
}