interrupt
下面是interrupt方法的文档的一部分:
1 * <p> If this thread is blocked in an invocation of the {@link
2 * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
3 * Object#wait(long, int) wait(long, int)} methods of the {@link Object}
4 * class, or of the {@link #join()}, {@link #join(long)}, {@link
5 * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
6 * methods of this class, then its interrupt status will be cleared and it
7 * will receive an {@link InterruptedException}.
View Doc
如果线程由于调用Object类的wait()方法、Thread类的join()实例方法以及Thread.sleep()静态方法而被挂起,此时其他线程调用了这个线程的interrupt方法,那么这个线程的中断状态会被清除,并会抛出一个InterruptedException异常。
从上面加粗的文字中可以得出两点结论:
1 即便调用了interrupt方法,之后用isInterrupted()方法检查它的中断状态时也不一定能得到true。
2 如果线程当前运行处的代码块不对InterruptedException异常进行合适的处理,那么interrupt方法就没有任何效果。
举一个例子来验证上面的结论:
1 public abstract class StoppableThread extends Thread {
2
3 @Override
4 public void run() {
5 while(!isInterrupted()){
6 System.out.println("isInterrupted: " + isInterrupted());
7 doWork();
8 }
9 System.out.println("END");
10 }
11
12 protected abstract void doWork();
13
14 public static void main(String[] args) {
15 StoppableThread thread = new StoppableThread() {
16
17 @Override
18 protected void doWork() {
19 System.out.println("running...");
20 try {
21 sleep(500);
22 } catch (InterruptedException e) {
23 e.printStackTrace();
24 }
25 }
26 };
27 thread.start();
28
29 try {
30 Thread.sleep(2200);
31 } catch (InterruptedException e) {
32 e.printStackTrace();
33 }
34
35 System.out.println("即将调用interrupt()方法");
36 thread.interrupt();
37 }
38 }
View Code
输出结果:
1 isInterrupted: false
2 running...
3 isInterrupted: false
4 running...
5 isInterrupted: false
6 running...
7 isInterrupted: false
8 running...
9 isInterrupted: false
10 running...
11 即将调用interrupt()方法
12 isInterrupted: false
13 running...
14 java.lang.InterruptedException: sleep interrupted
15 at java.lang.Thread.sleep(Native Method)
16 at readerandwriter.StoppableThread$1.doWork(StoppableThread.java:22)
17 at readerandwriter.StoppableThread.run(StoppableThread.java:9)
18 isInterrupted: false
19 running...
20 isInterrupted: false
21 running...
View Code
InterruptedException
interrupt方法所做的仅仅是设置了Thread对象的中断状态,InterruptedException是在线程的wait()、sleep()、join()等方法中抛出的。
修改上面的那个类的doWork()方法,让它声明抛出InterruptedException,并把实现中的sleep(500)去掉。
1 public abstract class StoppableThread extends Thread {
2
3 @Override
4 public void run() {
5 while(!isInterrupted()){
6 System.out.println("isInterrupted: " + isInterrupted());
7 try {
8 doWork();
9 } catch (InterruptedException e) {
10 System.out.println("InterruptedException is Thrown");
11 }
12 }
13 System.out.println("END");
14 }
15
16 protected abstract void doWork() throws InterruptedException;
17
18 public static void main(String[] args) {
19 StoppableThread thread = new StoppableThread() {
20
21 @Override
22 protected void doWork() throws InterruptedException{
23 System.out.println("running...");
24 }
25 };
26 thread.start();
27
28 try {
29 Thread.sleep(2200);
30 } catch (InterruptedException e) {
31 e.printStackTrace();
32 }
33
34 System.out.println("即将调用interrupt()方法");
35 thread.interrupt();
36 }
37 }
View Code
输出结果:
1 isInterrupted: false
2 running...
3 isInterrupted: false
4 running...
5 isInterrupted: false
6 running...
7 isInterrupted: false
8 running...
9 isInterrupted: false
10 running...
11 即将调用interrupt()方法
12 isInterrupted: false
13 running...
14 END
View Code
虽然线程结束了,但是InterruptedException异常并没有被抛出(如果抛出了会输出一行InterruptedException is Thrown)。线程是因为isInterrupted()返回true才退出循环的。结合上面interrupt方法的文档,可以得出两条结论:
1 interrupt仅仅设置了Thread对象的中断状态,InterruptedException是在线程的wait()、sleep()、join()等方法中抛出的,并且抛出后会清除掉Thread对象的中断状态。
2 当线程正在执行一些常规方法时,即便调用了interrupt方法也不会抛出InterruptedException,但会导致isInterrupted()方法返回true。
Thread.interrupted
Thread类的静态方法interrupted()用来检查当前线程的中断状态。这个方法除了返回当前线程的中断状态(true/false),还会把中断状态给清除掉。也就是说,如果连续调用两次这个方法,那么第二次必然返回的是false,因为无论中断状态原本是什么,它都已经被第一次的Thread.interrupted()清除了。
如何安全可靠地中断线程
没有一个完美的方法能够实现在调用某个方法的瞬间就让线程退出。
一个比较好的方法是在Thread子类中添加一个标志位,这里命名为isShutdownRequested,默认为false,并提供一个用于在interrupt的同时设置它为true的方法。最后,在线程的run()方法的while循环控制条件中利用这个标志位判断线程是否被要求中止。
下面提供一个StoppableThread实现,里面包含一个shutdown()方法,用于中止线程:
1 public abstract class StoppableThread extends Thread {
2 private volatile boolean isShutdownRequested = false;
3
4 public final void shutdown(){
5 isShutdownRequested = true;
6 interrupt();
7 }
8
9 public final boolean isShutdownRequested() {
10 return isShutdownRequested;
11 }
12
13 @Override
14 public void run() {
15 try {
16 while(!isShutdownRequested()){
17 doWork();
18 }
19 } catch (InterruptedException e) {
20 System.out.println("InterruptedException is Thrown");
21 } finally {
22 doShutdown();
23 }
24 }
25
26 protected abstract void doWork() throws InterruptedException;
27
28 protected abstract void doShutdown();
29
30
31 public static void main(String[] args) {
32 StoppableThread thread = new StoppableThread() {
33
34 @Override
35 protected void doWork() throws InterruptedException {
36 System.out.println("running...");
37 sleep(500);
38 }
39
40 @Override
41 protected void doShutdown() {
42 System.out.println("shutdown!");
43 }
44 };
45 thread.start();
46
47 try {
48 Thread.sleep(2200);
49 } catch (InterruptedException e) {
50 e.printStackTrace();
51 }
52
53 System.out.println("即将调用shutdown()方法");
54 thread.shutdown();
55 }
56 }
View Code
输出结果:
1 running...
2 running...
3 running...
4 running...
5 running...
6 即将调用shutdown()方法
7 InterruptedException is Thrown
8 shutdown!
View Code
参考资料
Thread的interrupt()方法排雷