interrupted()和isInterrupted()详述

使用方法

我们先验证中断异常响应,通过如下两种方法的使用示例来介绍,注意Runner中的run方法的部分区别

方法一

package com.liziba.p7;

import java.util.concurrent.TimeUnit;

/**

  • @Author: Liziba

  • @Date: 2021/6/24 21:05

*/

public class ThreadInterruptedDemo {

public static void main(String[] args) throws InterruptedException {

Thread t1 = new Thread(new Runner(), “Thread-01”);

t1.start();

// 主线程睡眠1秒,保证t1的充分执行

TimeUnit.SECONDS.sleep(1);

// 发起中断

t1.interrupt();

}

static class Runner implements Runnable {

@Override

public void run() {

while (!Thread.currentThread().isInterrupted()) {

System.out.println(Thread.currentThread().getName() + " is running .");

}

}

}

}

输出结果

可以看到线程在执行数次后终止运行

在这里插入图片描述

方法二

package com.liziba.p7;

import java.util.concurrent.TimeUnit;

/**

  • @Author: Liziba

  • @Date: 2021/6/24 21:18

*/

public class ThreadInterruptedDemo {

public static void main(String[] args) throws InterruptedException {

Thread t1 = new Thread(new Runner(), “Thread-01”);

t1.start();

// 主线程睡眠2秒,保证t1的充分执行

TimeUnit.SECONDS.sleep(1);

// 发起中断

t1.interrupt();

}

static class Runner implements Runnable {

@Override

public void run() {

while (!Thread.currentThread().isInterrupted()) {

System.out.println(Thread.currentThread().getName() + " is running .");

try {

// 睡眠2秒,保证主线程发起的中断能被捕获

TimeUnit.SECONDS.sleep(2);

} catch (InterruptedException e) {

// 不对中断做任何处理,try住异常,打印

e.printStackTrace();

}

}

}

}

}

输出结果

可以看到main线程中发起的t1线程中断,被捕获住异常后,未做任何处理,线程继续持续不断的运行

在这里插入图片描述

总结上述两种方式

方法一和方法二,均通过判断Thread.currentThread().isInterrupted()的值来运行run方法中的逻辑,Thread.currentThread().isInterrupted()在线程未中断时返回false,当main线程中执行 t1.interrupt()时,线程t1被中断,Thread.currentThread().isInterrupted()的值变为false;在方法一中,获取到这个变化后直接结束运行;在方法二中,由于sleep()使得线程阻塞会响应中断,但是此时我仅仅catch住异常,并没有对中断做任何处理,这里有个知识点是,线程响应中断抛出异常时,会恢复(清除)中断标志,所以t1.interrupt()对中断标志的修改又被恢复了,程序仍然不断的运行。

接下来我们来验证interrupted()对于中断的标志的清除

package com.liziba.p7;

import java.util.concurrent.TimeUnit;

/**

  •   isInterrupted()
    
  • @Author: Liziba

  • @Date: 2021/6/24 21:20

*/

public class ThreadInterruptDemo2 {

public static void main(String[] args) throws InterruptedException {

Thread thread = new Thread(new Runner(), “Thread-1”);

thread.start();

TimeUnit.SECONDS.sleep(2);

thread.interrupt();

}

static class Runner implements Runnable {

@Override

public void run() {

System.out.println(Thread.currentThread().getName() +" interrupted flag is " + Thread.currentThread().isInterrupted());

while (!Thread.currentThread().isInterrupted()) {

try {

System.out.println(Thread.currentThread().getName() + " is running .");

TimeUnit.SECONDS.sleep(1);

} catch (InterruptedException e) {

// 响应中断,抛出异常后中断位置会被复位,自己中断自己

Thread.currentThread().interrupt();

// 这里调用isInterrupted()获取当前的中断标志

System.out.println(Thread.currentThread().getName()

+" interrupted flag is " + Thread.currentThread().isInterrupted());

}

}

}

}

}

输出结果

这里证明interrupted()不清楚中断标志,线程在获取到 thread.interrupt()发起中断后,执行结束。

在这里插入图片描述

将上述catch中的Thread.currentThread().isInterrupted()修改为Thread.interrupted()再次运行

package com.liziba.p7;

import java.util.concurrent.TimeUnit;

/**

  • @Author: Liziba

  • @Date: 2021/6/24 21:23

*/

public class ThreadInterruptDemo2 {

public static void main(String[] args) throws InterruptedException {

Thread thread = new Thread(new Runner(), “Thread-1”);

thread.start();

TimeUnit.SECONDS.sleep(2);

thread.interrupt();

}

// 区别在catch中

static class Runner implements Runnable {

@Override

public void run() {

System.out.println(Thread.currentThread().getName() +" interrupted flag is " + Thread.currentThread().isInterrupted());

while (!Thread.currentThread().isInterrupted()) {

try {

System.out.println(Thread.currentThread().getName() + " is running .");

TimeUnit.SECONDS.sleep(1);

} catch (InterruptedException e) {

// 响应中断,抛出异常后中断位置会被复位,自己中断自己

Thread.currentThread().interrupt();

// 注意区别在这里

System.out.println(Thread.currentThread().getName()

+" interrupted flag is " + Thread.interrupted());

}

}

}

}

}

输出结果

线程也响应到了 thread.interrupt()的中断,但是由于catch中调用了Thread.interrupted(),对中断标志进行了清除,所以!Thread.currentThread().isInterrupted()判断仍然等于true,线程继续不断的运行。

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**

[外链图片转存中…(img-ayvFh00t-1715697264613)]

[外链图片转存中…(img-9qdF9Ren-1715697264614)]

[外链图片转存中…(img-qfNme3g9-1715697264614)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 8
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值