public class Practice83_Buffer {

private int balance = 1000000;

public synchronized int consume(int withdraw){

if(balance > withdraw){

balance -= withdraw;

}

return balance;

}

int getbal(){

return balance;

}

}


public class Practice83_Consume extends Thread{

private Practice83_Buffer b;

public Practice83_Consume(String name, Practice83_Buffer b){

super(name);

this.b = b;

}

public void run(){

try{

while(true){

int bal = b.consume(9800);

if(bal< 9800){

break;

}

System.out.println(getName() + " 9800원 인출, 남은 잔액 : " + bal);

sleep(100);

}

}catch(InterruptedException e){

System.out.println(e);}

}

}



public class Practice83_Consumer {

public static void main(String[] args){

Practice83_Buffer b = new Practice83_Buffer();

Practice83_Consume[] c = new Practice83_Consume[4];

for(int i = 0; i<4; i++){

c[i] = new Practice83_Consume("출금자" + (i+1), b);

c[i].start();

}

}


}


뭔가 에러나고 에러나고해서 고치고 뜯어고치고 갈아엎고해서 원하는 결과에 최대한 근접한 결과가 저건데..

남은잔액 10200에서 멈추고 끝남
이건 스레드쪽에서 while문 탈출하는 if구문이 println구문보다 앞에있어서 그런거같은데..
if구문을 println구문 뒤에다 놓으면 10200에서 9800을 뺀 결과물이 3번나옴;

내 머리로는 저 두개사이의 오류를 해결할 방안이 안나옴..
프로그래밍 초짜 살려주실분 없음?



P.S.

혹여나 필요할까봐 스레드쪽에서 while문 탈출시키는 break문 위치를 println문 위랑 아래에 있을때의 콘솔창값 첨부함