-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathStopThreadUnsafe.java
More file actions
48 lines (40 loc) · 1.15 KB
/
StopThreadUnsafe.java
File metadata and controls
48 lines (40 loc) · 1.15 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
/**
* @Project:
* @Author: leegoo
* @Date: 2019年07月10日
*/
package cn.withme.thread;
import lombok.Getter;
import lombok.Setter;
import javax.lang.model.type.UnknownTypeException;
import java.util.concurrent.TimeUnit;
/**
* ClassName: StopThreadUnsafe
*
* @author leegoo
* @Description: 线程停止方式1
* @date 2019年07月10日
*/
public class StopThreadUnsafe {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
while (true) {
if (Thread.currentThread().isInterrupted()) {
break;
}
int value = (int) System.currentTimeMillis() / 1000;
try {
System.err.println("value:" + value);
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("线程cathch.");
//e.printStackTrace();
}
}
});
thread.start();
Thread.sleep(3000);
thread.interrupt();
}
}