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()方法排雷

Java 多线程 interrupt方法的更多相关文章

  1. java多线程 interrupt(), interrupted(), isInterrupted()方法区别

    interrupt()方法: 作用是中断线程. 本线程中断自身是被允许的,且"中断标记"设置为true 其它线程调用本线程的interrupt()方法时,会通过checkAcces ...

  2. Java——多线程之方法详解

    Java多线程系列文章是Java多线程的详解介绍,对多线程还不熟悉的同学可以先去看一下我的这篇博客Java基础系列3:多线程超详细总结,这篇博客从宏观层面介绍了多线程的整体概况,接下来的几篇文章是对多 ...

  3. Java 多线程 sleep方法与wait方法的区别

    sleep方法会使线程暂停执行一段时间,wait方法会阻塞线程,直到被唤醒或等待时间超时. 两者区别具体如下: 1 原理不同 sleep方法是Thread类的静态方法,使线程暂停执行一段时间,等到计时 ...

  4. 多线程——interrupt方法

    测试interrupt()方法: package day_12_01_Thread; import java.util.Date; /** * 测试interrupt()方法:结束线程,但是线程还是活 ...

  5. Java多线程-run方法与start方法的区别

    package com.interview; /** * java多线程的两种实现方式以及run.start方法的区别 * @author MEI.LIU * */ public class Thre ...

  6. Java多线程-join方法

    thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程.比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B. 具体例子看链接 ...

  7. 多线程 interrupt()方法

    java interrupt()方法只是设置线程的中断标记,当对处于阻塞状态的线程调用interrupt方法时(处于阻塞状态的线程是调用sleep, wait, join 的线程),会抛出Interr ...

  8. 关于Java多线程-interrupt()、interrupted()、isInterrupted()解释

    多线程先明白一个术语"中断状态",中断状态为true,线程中断. interrupt():就是通知中止线程的,使"中断状态"为true. isInterrupt ...

  9. JAVA thread0.interrupt()方法

    interrupt()只是改变中断状态而已,interrupt()不会中断一个正在运行的线程.这一方法实际上完成的是,给受阻塞的线程抛出一个中断信号,这样受阻线程就得以退出阻塞的状态. 更确切的说,如 ...

随机推荐

  1. 018_nginx_proxy死循环问题

    今天线上遇到一个请求一次,触发多次的请求,而且直接把nginx机器压垮了.经排查,经过如下: 一. server{ server www.jyall.com; location /latestrele ...

  2. 为cobbler自动化安装系统工具添加epel源

    关于cobbler的安装及部署,参考:CentOS 6.5自动化运维之基于cobbler服务的自动化安装操作系统详解http://blog.csdn.net/reblue520/article/det ...

  3. 随机森林学习-sklearn

    随机森林的Python实现 (RandomForestClassifier) # -*- coding: utf- -*- """ RandomForestClassif ...

  4. Java连接oracle数据库的两种常用方法

    1. 使用thin连接 由于thin驱动都是纯Java代码,并且使用TCP/IP技术通过java的Socket连接上Oracle数据库,所以thin驱动是与平台无关的,你无需安装Oracle客户端,只 ...

  5. LeetCode(41):缺失的第一个正数

    Hard! 题目描述: 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输 ...

  6. php三种无限分类

    无限分类,是指从一个最高分类开始,每个子分类都可以分出自己的若干个子分类,可以一直分下去,称为无限级分类: 下面是对省市县的无限极分类的列子.数据库如图: 代码示例如下: /** * @Descrip ...

  7. django----对model查询扩展

    基于对象关联查询 一对多查询(Book--Publish): 正向查询,按字段: (从关联的表中查询) book_obj.publish : 与这本书关联的出版社对象 book_obj.publish ...

  8. 性能测试七:jmeter进阶之文件上传下载、定时器

    一.上传下载 上传: 1,POST请求,勾选 use …for post 2,同请求一起发送文件里,填写文件名称,参数名称 3,MIME类型: application/octet-stream(非必须 ...

  9. python 全栈开发,Day98(路飞学城背景,django ContentType组件,表结构讲解)

    昨日内容回顾 1. 为什么要做前后端分离? - 前后端交给不同的人来编写,职责划分明确. - API (IOS,安卓,PC,微信小程序...) - vue.js等框架编写前端时,会比之前写jQuery ...

  10. 【C++ Primer 第11章】4. 无序容器

    一.介绍 1. Hashtable和bucket 由于unordered_map内部采用的hashtable的数据结构存储,所以,每个特定的key会通过一些特定的哈希运算映射到一个特定的位置,我们知道 ...