package com.test0614;
class Demo11 implements Runnable{
private int bank=10000;
public void drawMoney(int m) {
if(bank >= m) {
bank -= m;
System.out.println("현재 스레드 :" + Thread.currentThread().getName()+":" + "잔액:" + bank);
}else if(bank==0){
System.out.println("현재 스레드 :" + Thread.currentThread().getName()+":" + "잔액이 부족합니다" );
}
}
@Override
public void run() {
synchronized (this) { // 현 객체 동기화
for(int i=1; i<=10; i++){
if(bank<=0){
this.notifyAll();
break;
}
drawMoney(1000); //1000원 인출
if(bank%2000==0){
try{
this.wait(); // 스레드를 멈춤
// 동기화 블럭에서만 사용가능
} catch(Exception e){
}
} else {
this.notify(); // wait()된 스레드를 깨움
}
}
}
}
}
public class Test11 {
public static void main(String[] args) {
Demo11 ob=new Demo11();
Thread t1=new Thread(ob);
Thread t2=new Thread(ob);
t1.start();
t2.start();
}
}
'자바 DB 연동 > 쓰레드' 카테고리의 다른 글
TimerTask (0) | 2013.06.19 |
---|---|
쓰레드 interrupt / isAlive (0) | 2013.06.19 |
데몬스레드 (0) | 2013.06.19 |
쓰레드 synchronized(동시에 일하는 것을 막는다) (0) | 2013.06.19 |
실시간으로 시간 보기 (0) | 2013.06.19 |