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();
}
}
}
P.S.
혹여나 필요할까봐 스레드쪽에서 while문 탈출시키는 break문 위치를 println문 위랑 아래에 있을때의 콘솔창값 첨부함
어 버퍼에 getbal이 왜 들어가있지; 일단 이거 삭제
꺄아아아아악 프로그래밍이다! 아아아아아악! 근데 그게 이렇게 길게나옴? - dc App
클래스 3개가 한덩어리인 프로그램이라 클래스 3개를 몽땅 묶어서 올린것..
... 다시 찬찬히 보다가 getbal을 만든 이유를 찾음; getbal넣고 마지막에 잔액표시하는거 짤막하게 추가댐