forked from brianway/java-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRun8_interrupted01.java
More file actions
68 lines (61 loc) · 1.49 KB
/
Copy pathRun8_interrupted01.java
File metadata and controls
68 lines (61 loc) · 1.49 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.brianway.learning.java.multithread.meet;
/**
* Created by brian on 2016/4/11.
*/
/**
* P25
* 判断线程是否停止状态
* 测试当前线程是否已经中断
*/
class MyThread8 extends Thread {
@Override
public void run() {
super.run();
for (int i = 0; i < 500; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("i=" + (i + 1));
}
}
}
public class Run8_interrupted01 {
public static void main(String[] args) {
try {
MyThread8 myThread8 = new MyThread8();
myThread8.start();
Thread.sleep(1000);
myThread8.interrupt();
System.out.println("Thread.interrupted(),是否停止1?=" + Thread.interrupted());
System.out.println("Thread.interrupted(),是否停止2?=" + Thread.interrupted());
} catch (InterruptedException e) {
System.out.println("main catch");
e.printStackTrace();
}
System.out.println("end");
}
}
/*
输出:
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.brianway.learning.java.multithread.meet.MyThread8.run(Run8_interrupted01.java:18)
Thread.interrupted(),是否停止1?=false
Thread.interrupted(),是否停止2?=false
end
i=10
i=11
省略....
-----------------
*/