-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadStopMainV3.java
More file actions
45 lines (32 loc) · 1.25 KB
/
Copy pathThreadStopMainV3.java
File metadata and controls
45 lines (32 loc) · 1.25 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
package thread.control.interrupt;
import util.MyLogger;
import static util.ThreadUtils.sleep;
public class ThreadStopMainV3 {
public static void main(String[] args) {
MyTask work = new MyTask();
Thread thread = new Thread(work, "work");
thread.start();
sleep(100);
MyLogger.log("작업 중지 요청 Interrupt");
thread.interrupt();
MyLogger.log("인터럽트 상태 1 " + thread.isInterrupted());
}
static class MyTask implements Runnable {
@Override
public void run() {
while(!Thread.currentThread().isInterrupted()) {
MyLogger.log("작업 중");
}
MyLogger.log("work 스레드 인터럽트 상태 = " + Thread.currentThread().isInterrupted() );
try {
MyLogger.log("자원 정리");
Thread.sleep(1000);
MyLogger.log("자원 정리 완료");
} catch (InterruptedException e) {
MyLogger.log("자원 정리 실패 - 자원 정리 중 인터럽트 발생");
MyLogger.log("work 스레드 인터럽트 상태3 = " + Thread.currentThread().isInterrupted());
}
MyLogger.log("자원 종료");
}
}
}