-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStopThreadTest01.java
More file actions
52 lines (45 loc) · 1.29 KB
/
StopThreadTest01.java
File metadata and controls
52 lines (45 loc) · 1.29 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
package thread;
/*
*
한 번 start된 쓰레드는 절대 다시 실행할 수 없다.
-> 일회용
쓰레드를 종료
1. 임의 변수를 선언해서 종료하는 방법
flag 변수
- 변수에 저장된 값에 따라서 작업을 처리할 용도 (실행 or 종료 - boolean 변수, int 변수...)
- 변수 값을 체크 ( 오래 걸리는 작업인 경우 중간에 stop할 수 있다.)
*/
class StopThread extends Thread{
private boolean state = true;
public void run() {
while(state) {
System.out.println("현재 쓰레드의 상태 : 실행중 ");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("현재 쓰레드 상태 : 종료상태");
};
public void stopThread() {
state = false;
}
}
public class StopThreadTest01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("===============메인쓰레드 시작===============");
StopThread t1 = new StopThread();
t1.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t1.stopThread();
System.out.println("===============메인쓰레드 종료===============");
}
}