-
Notifications
You must be signed in to change notification settings - Fork 122
/
InterruptDemo1.java
51 lines (45 loc) · 1.36 KB
/
InterruptDemo1.java
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
package multithreads.interruptThread;
/**
* main线程通过调用t.interrupt()从而通知t线程中断,
* 而此时t线程正位于hello.join()的等待中,此方法会立刻结束等待并抛出InterruptedException。
* 由于我们在t线程中捕获了InterruptedException,
* 因此,就可以准备结束该线程。在t线程结束前,
* 对hello线程也进行了interrupt()调用通知其中断。如果去掉这一行代码,
* 可以发现hello线程仍然会继续运行,且JVM不会退出。
*/
public class InterruptDemo1 {
public static void main(String[] args) throws InterruptedException {
Thread t = new MyThread2();
t.start();
Thread.sleep(1000);
t.interrupt(); // 中断t线程
t.join(); // 等待t线程结束
System.out.println("end");
}
}
class MyThread2 extends Thread {
public void run() {
Thread hello = new HelloThread2();
hello.start(); // 启动hello线程
try {
hello.join(); // 等待hello线程结束
} catch (InterruptedException e) {
System.out.println("interrupted!");
}
hello.interrupt();
}
}
class HelloThread2 extends Thread {
public void run() {
int n = 0;
while (!isInterrupted()) {
n++;
System.out.println(n + " hello!");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
break;
}
}
}
}