-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestInterrupt.java
More file actions
29 lines (28 loc) · 996 Bytes
/
TestInterrupt.java
File metadata and controls
29 lines (28 loc) · 996 Bytes
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
package Thread;
public class TestInterrupt {
public static void main(String[] args) {
Thread thread = new Thread("T1"){
@Override
public void run() {
while (true){
if(Thread.currentThread().isInterrupted())
{
System.out.println("Interruted!");
break;
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println("Interruted When Sleep");
//设置中断状态,抛出异常后会清除中断标记位
Thread.currentThread().interrupt();
}
Thread.yield();
}
}
};
System.out.println(thread.isInterrupted());
thread.interrupt();
System.out.println(thread.isInterrupted());
}
}