forked from liujiboy/Java_Course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountThread.java
More file actions
49 lines (40 loc) · 1.04 KB
/
AccountThread.java
File metadata and controls
49 lines (40 loc) · 1.04 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
package code0905;
public class AccountThread extends Thread {
WrongAccount account;
int delay = 100;
public AccountThread(WrongAccount account) {
super();
this.account = account;
}
@Override
public void run() {
// 下面执行取款操作
if (account.balance >= 1000) {
try {
sleep(delay);
account.balance -= 1000;
System.out.println("取款1000成功。");
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
System.out.println("取款失败。");
}
}
public static void main(String[] args) throws InterruptedException {
// 创建账号对象
WrongAccount account = new WrongAccount(1005);
// 创建三个取款线程
AccountThread t1 = new AccountThread(account);
AccountThread t2 = new AccountThread(account);
AccountThread t3 = new AccountThread(account);
t1.start();
t2.start();
t3.start();
// 主线程等待三个线程结束后,输出最后余额
t1.join();
t2.join();
t3.join();
System.out.println("最终账号余额是:" + account.balance);
}
}