-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankAccountV3.java
More file actions
40 lines (32 loc) · 1.24 KB
/
Copy pathBankAccountV3.java
File metadata and controls
40 lines (32 loc) · 1.24 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
package thread.sync;
import util.MyLogger;
import util.ThreadUtils;
public class BankAccountV3 implements BankAccount{
private int balance;
public BankAccountV3(int initBalance) {
this.balance = initBalance;
}
@Override
public boolean withdraw(int amount) {
MyLogger.log("거래 시작 : " + getClass().getSimpleName());
synchronized(this) {
// 잔고가 출금액보다 적으면, 진행 안됨
MyLogger.log("[검증 시작] 출금액 : " + amount + ", 잔액 : " + balance);
if (balance < amount) {
MyLogger.log("[검증 실패] 출금액 : " + amount + ", 잔액 : " + balance);
return false;
}
// 잔고가 출금액 보다 많으면, 진행
MyLogger.log("[검증 완료] 출금액 : " + amount + ", 잔액 : " + balance);
ThreadUtils.sleep(1000); // 출금에 걸리는 시간으로 가정
balance = balance - amount;
MyLogger.log("[출금 완료] 출금액 : " + amount + ", 변경 잔액 : " + balance);
}
MyLogger.log("거래 종료");
return true;
}
@Override
public synchronized int getBalance() {
return balance;
}
}