interrupted()和isInterrupted()详述(1)

方法一

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,线程继续不断的运行。

在这里插入图片描述

总结

Android架构学习进阶是一条漫长而艰苦的道路,不能靠一时激情,更不是熬几天几夜就能学好的,必须养成平时努力学习的习惯。所以:贵在坚持!

上面分享的字节跳动公司2020年的面试真题解析大全,笔者还把一线互联网企业主流面试技术要点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。

就先写到这,码字不易,写的很片面不好之处敬请指出,如果觉得有参考价值的朋友也可以关注一下我

①「Android面试真题解析大全」PDF完整高清版+②「Android面试知识体系」学习思维导图压缩包阅读下载,最后觉得有帮助、有需要的朋友可以点个赞


《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
的面试真题解析大全,笔者还把一线互联网企业主流面试技术要点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。

就先写到这,码字不易,写的很片面不好之处敬请指出,如果觉得有参考价值的朋友也可以关注一下我

①「Android面试真题解析大全」PDF完整高清版+②「Android面试知识体系」学习思维导图压缩包阅读下载,最后觉得有帮助、有需要的朋友可以点个赞

[外链图片转存中…(img-sMmM4xir-1715838008575)]

[外链图片转存中…(img-AExNzRVH-1715838008579)]

[外链图片转存中…(img-lmy4XSVk-1715838008581)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值