개발하자/JAVA고급
쓰레드 안전하게 종료시키는 방법
i구야
2015. 4. 2. 11:10
class Echo extends Thread {
private int waitTime; // in millisecs
public Echo(String word, int waitTime) {
super(word);
this.waitTime = waitTime;
}
public void run( ) {
for(int i=0; i<10; i++) {
System.out.print(getName()+">"+i+"\t");
try {
sleep(waitTime);
}catch (InterruptedException e){
System.out.print(getName()+":인터럽트발생\t");
break;//인터럽트 발생하면 run을 안전하게 종료시키는 방법
}
}
}
public static void main(String[] args) {
Echo e1 = new Echo("Foo", 5000);
Echo e2 = new Echo("Bar", 5000);
e1.start(); e1.interrupt(); e2.start(); e2.interrupt();
}
}