class Account{
private int tot = 1000000;
synchronized boolean Payment(int pay) {
if(pay<tot){
tot -= pay;
return true;
}
else{
return false;
}
}
int gettot() {
return tot;
}
}
class Customer extends Thread {
Account acc1;
Customer(Account acc){
this.acc1 = acc;
}
public void run() {
try{
while(acc1.Payment(9800)) {
System.out.println(getName() + " 9800원 인출, 남은잔액 : " + acc1.gettot());
sleep(50);
}
}catch(Exception e){
System.out.println(e);
}
}
}
public class 과제1번 {
public static void main(String[] args){
Account acc = new Account();
Customer[] cust = new Customer[4];
for(int i=0; i<4; i++){
cust[i] = new Customer(acc);
cust[i].start();
}
for(int i=0; i<4; i++){
try {
cust[i].join();
}catch(InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("잔액부족.남은금액:" + acc.gettot() + ", 인출금액 : 9800");
System.out.println("총액은 : " + acc.gettot());
}
임계영역을 지정해줘서 각 쓰레드가 9800원씩 출금을 하는 내용인데 왜 겹치는 부분이 있는지 궁금합니다. 해결 방법도 궁금합니다.