六.1、interrupt方法的介绍
发布日期:2021-09-12 09:57:57 浏览次数:33 分类:技术文章

本文共 3200 字,大约阅读时间需要 10 分钟。

interrupt方法的引出

interrupt()不能中断在运行中的线程,它只能改变中断状态而已。

public class InterruptionInJava implements Runnable{
public static void main(String[] args) throws InterruptedException {
Thread testThread = new Thread(new InterruptionInJava(),"InterruptionInJava"); //start thread testThread.start(); Thread.sleep(1000); //interrupt thread testThread.interrupt(); System.out.println("main end"); } @Override public void run() {
while(true){
if(Thread.currentThread().isInterrupted()){
System.out.println("Yes,I am interruted,but I am still running"); }else{
System.out.println("not yet interrupted"); } } }}

结果显示,被中断后,仍旧运行,不停打印Yes,I am interruted,but I am still running

那么,如何正确中断?

既然是只能修改中断状态,那么我们应该针对中断状态做些什么。

不禁又要发出天问,如何中断线程?答案是添加一个开关。

public class InterruptionInJava implements Runnable{
private volatile static boolean on = false; public static void main(String[] args) throws InterruptedException {
Thread testThread = new Thread(new InterruptionInJava(),"InterruptionInJava"); //start thread testThread.start(); Thread.sleep(1000); InterruptionInJava.on = true; System.out.println("main end"); } @Override public void run() {
while(!on){
if(Thread.currentThread().isInterrupted()){
System.out.println("Yes,I am interruted,but I am still running"); }else{
System.out.println("not yet interrupted"); } } }}

这种开关的方式看起来包治百病,但是当遇到线程阻塞时,就会很无奈了,正如下面代码所示:

public class InterruptionInJava implements Runnable{
private volatile static boolean on = false; public static void main(String[] args) throws InterruptedException {
Thread testThread = new Thread(new InterruptionInJava(),"InterruptionInJava"); //start thread testThread.start(); Thread.sleep(1000); InterruptionInJava.on = true; System.out.println("main end"); } @Override public void run() {
while(!on){
try {
Thread.sleep(Integer.MAX_VALUE); } catch (InterruptedException e) {
System.out.println("caught exception: "+e); } } }}

线程被阻塞无法被中断。这时候救世主interrupt函数又回来了,它可以迅速中断被阻塞的线程,抛出一个InterruptedException,把线程从阻塞状态中解救出来,show the code。

public class InterruptionInJava implements Runnable{
private volatile static boolean on = false; public static void main(String[] args) throws InterruptedException {
Thread testThread = new Thread(new InterruptionInJava(),"InterruptionInJava"); //start thread testThread.start(); Thread.sleep(1000); InterruptionInJava.on = true; testThread.interrupt(); System.out.println("main end"); } @Override public void run() {
while(!on){
try {
Thread.sleep(10000000); } catch (InterruptedException e) {
System.out.println("caught exception right now: "+e); } } }}

转载地址:https://blog.csdn.net/weixin_38367817/article/details/103812524 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:深入理解Java虚拟机(可以不看)
下一篇:六、取消与关闭

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月16日 23时34分18秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

【面试篇】数据结构-哈希表 2019-04-26
【Leetcode刷题篇】leetcode88 合并两个有序数组 2019-04-26
【Leetcode刷题篇】剑指offer51 数组中的逆序对 2019-04-26
【Leetcode刷题篇】剑指offer55-平衡二叉树 2019-04-26
【Leetcode刷题篇】leetcode98 判断一棵树是否为二叉搜索树 2019-04-26
Java中arraylist和数组的相互转换 2019-04-26
【Leetcode刷题篇 】leetcode147 对链表进行插入排序 2019-04-26
【Leetcode刷题篇】leetcode148 排序链表 2019-04-26
【面试篇】Java中String、StringBuilder与StringBuffer的区别? 2019-04-26
【面试篇】Java对象的hashCode()相同,equals()一定为true吗? 2019-04-26
【面试篇】Java中static和final关键字的作用是什么? 2019-04-26
【面试篇】Java中接口和抽象类的区别是什么? 2019-04-26
【Java网络编程与IO流】Java中IO流分为几种?字符流、字节流、缓冲流、输入流、输出流、节点流、处理流 2019-04-26
【Java网络编程与IO流】Java中BIO、NIO、AIO的区别是什么? 2019-04-26
【Leetcode刷题篇】leetcode136 只出现一次的数字 2019-04-26
spring boot整合thymeleaf,支持JSP和HTML页面开发 2019-04-26
【Java网络编程与IO流】Spring boot整合SSE实现服务器实时推送流信息 2019-04-26
【Java网络编程与IO流】SpringBoot + WebSocket + Netty实现实时的服务器消息推送 2019-04-26
【Leetcode刷题篇】leetcode141 环形链表II 2019-04-26
【Leetcode刷题篇】leetcode160 相交链表 2019-04-26