티스토리 뷰

public class InterruptTest {


public static void main(String[] args) {

// TODO Auto-generated method stub


ThreadInterrupt th = new ThreadInterrupt("Thread");

th.start();

try {

Thread.sleep(2000);

th.interrupt(); //2초 뒤 인터럽트

} catch (InterruptedException e) {

}

}

}


class ThreadInterrupt extends Thread {

ThreadInterrupt(String str) {

super(str);

}


public void run() {

try {

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

Thread.sleep(500);

System.out.println(getName() + " " + i + "번째 수행");

}

} catch (InterruptedException e) {

System.out.println("스레드 강제 종료");

//인터럽트 예외를 받는다.

return;

}

}

}


결과 : 

Thread 0번째 수행

Thread 1번째 수행

Thread 2번째 수행

Thread 3번째 수행

스레드 강제 종료



'java,web study > 3주차 (7월 15일 ~21일)' 카테고리의 다른 글

Migration  (0) 2013.07.17
멀티스레드  (0) 2013.07.17
데몬 스레드  (0) 2013.07.17
스레드 예제 (성금 모금)  (0) 2013.07.17
스레드 예제 (Bank)  (0) 2013.07.17
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함