-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterrupted.java
More file actions
43 lines (39 loc) · 1.24 KB
/
Interrupted.java
File metadata and controls
43 lines (39 loc) · 1.24 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
package ThreadDemo;
/**
* @author:Gerry
* @description:
* @date: Created in 2018/12/25
*/
public class Interrupted {
public static void main(String[] args) throws Exception {
//sleepThread不停的尝试睡眠
Thread sleepThread = new Thread(new SleepRunner(), "SleepThread");
sleepThread.setDaemon(true);
//busyThread不停的运行
Thread busyThread = new Thread(new BusyRunner(), "BusyThread");
busyThread.setDaemon(true);
sleepThread.start();
busyThread.start();
// 休眠5秒,让sleepThread和busyThread充分运行
SleepUtils.second(5);
sleepThread.interrupt();
busyThread.interrupt();
System.out.println("SleepThread interrupted is " + sleepThread.isInterrupted());
System.out.println("BusyThread interrupted is " + busyThread.isInterrupted());
// 防止sleepThread和busyThread立刻退出
SleepUtils.second(2);
}
static class SleepRunner implements Runnable {
@Override
public void run() {
SleepUtils.second(10);
}
}
static class BusyRunner implements Runnable {
@Override
public void run() {
while (true) {
}
}
}
}