-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStopThreadTest2.java
More file actions
56 lines (44 loc) · 1.36 KB
/
StopThreadTest2.java
File metadata and controls
56 lines (44 loc) · 1.36 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package thread;
/*
쓰레드 종료
2. 인터럽트를 발생시키고 현재 상태를 확인한 후 작업하기
*/
class StopThread02 implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
try {
while(!Thread.currentThread().isInterrupted()) {
System.out.println("현재 쓰레드 상태 : 실행 중");
Thread.sleep(500);
}
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
} finally {
System.out.println("현재 쓰레드 상태 : 종료");
}
}
}
public class StopThreadTest2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("===============메인쓰레드 시작===============");
Thread t= new Thread(new StopThread02());
t.start();
System.out.println("쓰레드 이름 :"+t.getName());
System.out.println("인터럽트상태 :"+t.isInterrupted());
System.out.println("인터럽트상태 :"+t.isInterrupted());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 인터럽트 발생
t.interrupt();
System.out.println("인터럽트상태 :"+t.isInterrupted());
System.out.println("인터럽트상태 :"+t.isInterrupted());
System.out.println("===============메인쓰레드 종료===============");
}
}