-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterruptTest.java
More file actions
43 lines (37 loc) · 1.15 KB
/
InterruptTest.java
File metadata and controls
43 lines (37 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
package Thread.interrupt;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/*
* P699练习18:一个非任务的类,中用一个时间较长的sleep方法,在一个任务类中调用这非任务类的方法。在main中,启动这个任务,调用interrupt
* 方法,确定安全的关闭这个任务
*
* */
public class InterruptTest implements Runnable{
private SleepClass sleepClass;
public InterruptTest(SleepClass sleepClass){
this.sleepClass=sleepClass;
}
public void run() {
sleepClass.SleepMethod();
System.out.println("安全跳出中断");
}
public static void main(String[] args) {
/*ExecutorService service=Executors.newCachedThreadPool();
service.execute(new InterruptTest(new SleepClass()));
service.shutdown();*/
Thread thread=new Thread(new InterruptTest(new SleepClass()));
thread.start();
thread.interrupt();
}
}
class SleepClass{
public void SleepMethod(){
try {
TimeUnit.MICROSECONDS.sleep(1000*100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("sleep is interrupted");
}
}
}