InterruptException
之所以转载这篇博客是因为在公司的时候和同事讨论什么时候会发生interruptException,感觉自己的思路不是很清晰
中断线程详解(Interrupt)
官方文档中对此有详细说明:《为何不赞成使用 Thread.stop、Thread.suspend 和 Thread.resume?》。在此引用stop方法的说明:
1、通过修改共享变量来通知目标线程停止运行
private Thread blinker; public void start() { blinker = new Thread(this); blinker.start();
} public void stop() { blinker.stop(); // UNSAFE!
} public void run() { Thread thisThread = Thread.currentThread(); while (true) { try { thisThread.sleep(interval); } catch (InterruptedException e){ } repaint(); }
}
你可以使用如下方式避免使用Thread.stop方法:
private volatile Thread blinker; public void stop() { blinker = null;
} public void run() { Thread thisThread = Thread.currentThread(); while (blinker == thisThread) { try { thisThread.sleep(interval); } catch (InterruptedException e){ } repaint(); }
}
package com.polaris.thread; public class TestThread implements Runnable{ boolean stop = false; public static void main(String[] args) throws Exception { Thread thread = new Thread(new TestThread(),"My Thread"); System.out.println( "Starting thread..." ); thread.start(); Thread.sleep( 3000 ); System.out.println( "Interrupting thread..." ); thread.interrupt(); System.out.println("线程是否中断:" + thread.isInterrupted()); Thread.sleep( 3000 ); System.out.println("Stopping application..." ); } public void run() { while(!stop){ System.out.println( "My Thread is running..." ); // 让该循环持续一段时间,使上面的话打印次数少点 long time = System.currentTimeMillis(); while((System.currentTimeMillis()-time < 1000)) { } } System.out.println("My Thread exiting under request..." ); }
}
运行后的结果是:
Starting thread...
My Thread is running...
My Thread is running...
My Thread is running...
My Thread is running...
Interrupting thread...
线程是否中断:true
My Thread is running...
My Thread is running...
My Thread is running...
Stopping application...
My Thread is running...
My Thread is running...
……
其实对于这些情况有一个通用的处理方法:
package com.polaris.thread; public class TestThread2 implements Runnable{ boolean stop = false; public static void main(String[] args) throws Exception { Thread thread = new Thread(new TestThread2(),"My Thread2"); System.out.println( "Starting thread..." ); thread.start(); Thread.sleep( 3000 ); System.out.println( "Interrupting thread..." ); thread.interrupt(); System.out.println("线程是否中断:" + thread.isInterrupted()); Thread.sleep( 3000 ); System.out.println("Stopping application..." ); } public void run() { while(!stop){ System.out.println( "My Thread is running..." ); // 让该循环持续一段时间,使上面的话打印次数少点 long time = System.currentTimeMillis(); while((System.currentTimeMillis()-time < 1000)) { } if(Thread.currentThread().isInterrupted()) { return; } } System.out.println("My Thread exiting under request..." ); }
}
![](/assets/blank.gif)
注意:interrupted与isInterrupted方法的区别(见API文档)
引用一篇文章
来自随心所欲http://redisliu.blog.sohu.com/131647795.html 的《Java的interrupt机制》
当外部线程对某线程调用了thread.interrupt()方法后,java语言的处理机制如下:
如果该线程处在可中断状态下,(调用了xx.wait(),或者Selector.select(),Thread.sleep()等特定会发生阻塞的api),那么该线程会立即被唤醒,同时会受到一个InterruptedException,同时,如果是阻塞在io上,对应的资源会被关闭。如果该线程接下来不执行“Thread.interrupted()方法(不是interrupt),那么该线程处理任何io资源的时候,都会导致这些资源关闭。当然,解决的办法就是调用一下interrupted(),不过这里需要程序员自行根据代码的逻辑来设定,根据自己的需求确认是否可以直接忽略该中断,还是应该马上退出。
如果该线程处在不可中断状态下,就是没有调用上述api,那么java只是设置一下该线程的interrupt状态,其他事情都不会发生,如果该线程之后会调用行数阻塞API,那到时候线程会马会上跳出,并抛出InterruptedException,接下来的事情就跟第一种状况一致了。如果不会调用阻塞API,那么这个线程就会一直执行下去。除非你就是要实现这样的线程,一般高性能的代码中肯定会有wait(),yield()之类出让cpu的函数,不会发生后者的情况。
InterruptException相关推荐
- Java 多线程编程之 interruptException
下面是java 多线程中的异常处理: package multithread; public class InterruptException { public static void main(St ...
- interrupt InterruptException
JDK1.6中的interrupt函数: public void interrupt() 中断线程 如果当前线程没有中断它自己(这在任何情况下都是允许的),则该线程的 checkAccess 方法就 ...
- InterruptException处理方法
1.背景: 项目中tryLock()方法带参数时,例如:rLock.tryLock(0, 10, TimeUnit.SECONDS)会抛出InterruptException, 那么,遇到Interr ...
- interrupt()方法和InterruptException异常
interrupt()方法和InterruptException异常,是java专门用来处理线程阻塞的.线程阻塞,就表示要等待一段时间.如果需要等待的时间比较长,正常还没结束之前想中断某个线程的阻塞状 ...
- kafka 异步发送阻塞_Kafka学习一
一.github下载kafka的源码 可以看到kafka的源码开源社区是非常活跃的. 二.搭建kafka环境 构建kafka环境,首先需要安装Scala和gradle,再安装的scala插件需要和你的 ...
- java并发编程实战:第十六章----Java内存模型
一.什么是内存模型,为什么要使用它 如果缺少同步,那么将会有许多因素使得线程无法立即甚至永远看到一个线程的操作结果 编译器把变量保存在本地寄存器而不是内存中 编译器中生成的指令顺序,可以与源代码中的顺 ...
- 为什么线程被唤醒后锁会被抢?
Java线程等待唤醒机制(加深理解) 等待队列 调用obj的wait(), notify()方法前,必须获得obj锁,也就是必须写在synchronized(obj) 代码段内. 与等待队列相关的步骤 ...
- OMG!Semaphore里面居然有这么一个大坑!
作者 | why技术 来源 | why技术(ID:hello_hi_why) 荒腔走板 上周写了一篇文章,一不小心戳到了大家的爽点,其中一个转载我文章的大号,阅读量居然突破了 10w+,我也是受宠若惊 ...
- 【转载】并发数据结构
2019独角兽企业重金招聘Python工程师标准>>> 本文转载自http://shift-alt-ctrl.iteye.com/blog/1841084 请首先参考:http:// ...
最新文章
- 直播APP常用动画效果
- 180 所高校新增“人工智能”专业,人工智能火到爆!
- 阿里云数据中台全新产品DataTrust聚焦企业数据安全保障
- Java学习lesson 02
- chemdraw怎么连接两个结构_利用神经结构搜索构建快速准确轻量级的超分辨率网络...
- Leetcode--621. 任务调度器
- Java讲课笔记23:Map接口及其实现类
- 准确度判断 语义分割_【语义分割】DeepLab v1/v2
- 自编码器图像去噪matlab,深度有趣 | 05 自编码器图像去噪
- Cache之直接映射
- java indexof 参数_Java indexOf() 方法
- HTML基础开头代码
- 人工智能--框架表示法
- 数据管理知识体系指南(第二版)-第四章——数据架构-学习笔记
- MiniGUI——第一个程序helloworld
- SXT:聚合与组合的理解
- 【UV打印机】RYPC打印软件教程(三)-参数设置
- 创建并配置一个伪分布式Hadoop3.x版本集群(三)
- 剑指offer面试题(31-40)——java实现
- 解决echarts设置x轴、y轴刻度起始值、终止值以及步长