Java 多线程 interrupt方法

  interrupt

  下面是interrupt方法的文档的一部分:

 * <p> If this thread is blocked in an invocation of the {@link
* Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
* Object#wait(long, int) wait(long, int)} methods of the {@link Object}
* class, or of the {@link #join()}, {@link #join(long)}, {@link
* #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
* methods of this class, then its interrupt status will be cleared and it
* will receive an {@link InterruptedException}.

View Doc

  如果线程由于调用Object类的wait()方法、Thread类的join()实例方法以及Thread.sleep()静态方法而被挂起,此时其他线程调用了这个线程的interrupt方法,那么这个线程的中断状态会被清除,并会抛出一个InterruptedException异常。

  从上面加粗的文字中可以得出两点结论:

  1 即便调用了interrupt方法,之后用isInterrupted()方法检查它的中断状态时也不一定能得到true。

  2 如果线程当前运行处的代码块不对InterruptedException异常进行合适的处理,那么interrupt方法就没有任何效果。

  举一个例子来验证上面的结论:

 public abstract class StoppableThread extends Thread {

     @Override
public void run() {
while(!isInterrupted()){
System.out.println("isInterrupted: " + isInterrupted());
doWork();
}
System.out.println("END");
} protected abstract void doWork(); public static void main(String[] args) {
StoppableThread thread = new StoppableThread() { @Override
protected void doWork() {
System.out.println("running...");
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start(); try {
Thread.sleep(2200);
} catch (InterruptedException e) {
e.printStackTrace();
} System.out.println("即将调用interrupt()方法");
thread.interrupt();
}
}

  输出结果:

 isInterrupted: false
running...
isInterrupted: false
running...
isInterrupted: false
running...
isInterrupted: false
running...
isInterrupted: false
running...
即将调用interrupt()方法
isInterrupted: false
running...
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at readerandwriter.StoppableThread$1.doWork(StoppableThread.java:22)
at readerandwriter.StoppableThread.run(StoppableThread.java:9)
isInterrupted: false
running...
isInterrupted: false
running...

  

  InterruptedException

  interrupt方法所做的仅仅是设置了Thread对象的中断状态,InterruptedException是在线程的wait()、sleep()、join()等方法中抛出的。

  修改上面的那个类的doWork()方法,让它声明抛出InterruptedException,并把实现中的sleep(500)去掉。

 public abstract class StoppableThread extends Thread {

     @Override
public void run() {
while(!isInterrupted()){
System.out.println("isInterrupted: " + isInterrupted());
try {
doWork();
} catch (InterruptedException e) {
System.out.println("InterruptedException is Thrown");
}
}
System.out.println("END");
} protected abstract void doWork() throws InterruptedException; public static void main(String[] args) {
StoppableThread thread = new StoppableThread() { @Override
protected void doWork() throws InterruptedException{
System.out.println("running...");
}
};
thread.start(); try {
Thread.sleep(2200);
} catch (InterruptedException e) {
e.printStackTrace();
} System.out.println("即将调用interrupt()方法");
thread.interrupt();
}
}

  输出结果:

 isInterrupted: false
running...
isInterrupted: false
running...
isInterrupted: false
running...
isInterrupted: false
running...
isInterrupted: false
running...
即将调用interrupt()方法
isInterrupted: false
running...
END

  虽然线程结束了,但是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()方法,用于中止线程:

 public abstract class StoppableThread extends Thread {
private volatile boolean isShutdownRequested = false; public final void shutdown(){
isShutdownRequested = true;
interrupt();
} public final boolean isShutdownRequested() {
return isShutdownRequested;
} @Override
public void run() {
try {
while(!isShutdownRequested()){
doWork();
}
} catch (InterruptedException e) {
System.out.println("InterruptedException is Thrown");
} finally {
doShutdown();
}
} protected abstract void doWork() throws InterruptedException; protected abstract void doShutdown(); public static void main(String[] args) {
StoppableThread thread = new StoppableThread() { @Override
protected void doWork() throws InterruptedException {
System.out.println("running...");
sleep(500);
} @Override
protected void doShutdown() {
System.out.println("shutdown!");
}
};
thread.start(); try {
Thread.sleep(2200);
} catch (InterruptedException e) {
e.printStackTrace();
} System.out.println("即将调用shutdown()方法");
thread.shutdown();
}
}

  输出结果:

 running...
running...
running...
running...
running...
即将调用shutdown()方法
InterruptedException is Thrown
shutdown!

  

  参考资料

  Thread的interrupt()方法排雷

上一篇:vue项目对axios的全局配置


下一篇:04_web基础(二)之web构建