File tree

5 files changed

+24
-18
lines changed

5 files changed

+24
-18
lines changed
Some generated files are not rendered by default. Learn more about customizing how changed files appear on .
Some generated files are not rendered by default. Learn more about customizing how changed files appear on .
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
public interface Account {
1111
// 获取余额
1212
Integer getBalance();
13+
1314
// 取款
1415
void withdraw(Integer amount);
16+
1517
/**
1618
* 方法内会启动 1000 个线程,每个线程做 -10 元 的操作
1719
* 如果初始余额为 10000 那么正确的结果应当是 0
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @Description:CAS无锁保证安全
99
*/
1010
public class Account_CAS implements Account {
11+
1112
private AtomicInteger balance;
1213

1314
public Account_CAS(Integer balance) {
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
* @Description:synchronized加锁保证安全
77
*/
88
public class Account_Sync implements Account{
9+
910
private Integer balance;
11+
1012
public Account_Sync(Integer balance) {
1113
this.balance = balance;
1214
}
15+
1316
@Override
1417
public synchronized Integer getBalance() {
1518
return balance;
1619
}
20+
1721
@Override
1822
public synchronized void withdraw(Integer amount) {
1923
balance -= amount;

0 commit comments

Comments
 (0)