Thread.currentThread().interrupt()和Thread.interrupted()和Thread.currentThread().isInterrupted()
Thread.interrupted() 可以返回当前标志位并使标志位为false
Thread.currentThread().interrupt() 可以使标志位置为true
Thread.currentThread().isInterrupted() 返回当前线程标志位
如果线程中使用了sleep,wait,当调用thread.interrupt()的时候,会使当前线程中断标志位值为false,
并触发异常InterruptedException
public static void main(String[] args) {AtomicInteger atomicInteger = new AtomicInteger(0);Thread thread = new Thread(() -> {for (; ; ) {try {Thread.sleep(2000);} catch (InterruptedException e) {Thread.currentThread().interrupt();System.out.println("InterruptedException "+Thread.currentThread().isInterrupted());boolean interrupted = Thread.interrupted();System.out.println("interrupted===="+interrupted);}int i = atomicInteger.incrementAndGet();if (i==2){Thread.currentThread().interrupt();
// boolean interrupted = Thread.interrupted();
// System.out.println("interrupted===="+interrupted);}if (Thread.currentThread().isInterrupted()) {System.out.println("Thread.currentThread().isInterrupted() "+Thread.currentThread().isInterrupted());if (i==80000){boolean interrupted = Thread.interrupted();System.out.println("interrupted===="+interrupted);}
// boolean interrupted = Thread.interrupted();
// System.out.println("interrupted===="+interrupted);}System.out.println(Thread.currentThread().isInterrupted());// if (Thread.currentThread().isInterrupted()) {// System.out.println(Thread.currentThread().isInterrupted());
// }}});thread.start();
// try {// Thread.sleep(5000);
// } catch (InterruptedException e) {// e.printStackTrace();
// }System.out.println("Interrupted");
// Thread.interrupted();
// thread.interrupt();
// thread.in}
Thread.currentThread().interrupt()和Thread.interrupted()和Thread.currentThread().isInterrupted()相关推荐
- Thread.currentThread.interrupt()
Thread.currentThread.interrupt() 只对阻塞线程起作用, 当线程阻塞时,调用interrupt方法后,该线程会得到一个interrupt异常,可以通过对该异常的处理而退出 ...
- 简述Thread的interrupt()、interrupted()及isInterrupted()的区别
前言 在java Thread类中,我们会看到interrupt().interrupted()及isInterrupted(),在大多数情况下,我们都不会使用到它们,但是有一个Interrupted ...
- Thread.currentThread().interrupt()
https://www.zhihu.com/question/41048032 作者:Intopass 链接:https://www.zhihu.com/question/41048032/answe ...
- Thread.interrupted()与Thread.currentThread().isInterrupted()区别
背景 今天在研究ThreadPoolExecutor源码时,在runWorker(Worker w)的方法中有一个判断条件分别用到了Thread.interrupted()和Thread.curren ...
- Thread类中interrupt()、interrupted()和isInterrupted()方法详解
首先看看官方说明: interrupt()方法 其作用是中断此线程(此线程不一定是当前线程,而是指调用该方法的Thread实例所代表的线程),但实际上只是给线程设置一个中断标志,线程仍会继续运行. i ...
- Thread中interrupt()interrupted()和isInterrupted()的区别
2019独角兽企业重金招聘Python工程师标准>>> 在java线程中,线程的状态分为6种.官方文档的解释是: /*** Thread state for a thread whi ...
- Thread类的interrupt(),interrupted(),isInterrupted()
1. sleep() & interrupt() 线程A正在使用sleep()暂停着: Thread.sleep(100000); 如果要取消他的等待状态,可以在正在执行的线程里(比如这里是B ...
- Thread.interrupted()与Thread.isInterrupted()的区别
调用Thread.interrupt()方法并不能真正停止线程,只是在当前线程做了一个中断的状态标志. public class MyThread extends Thread{@Overridepu ...
- Java Thread的interrupt方法详解
Java Thread的interrupt方法详解 一.概述 interrupt方法的目的是给线程发出中断信号,但是不保证线程真的会中断 中断一个线程只是为了引起该线程的注意,被中断线程可以决定如何应 ...
最新文章
- pytorch指定gpu
- 模拟登录123026网站
- hdu 4291 矩阵幂 循环节
- HDU - 6749 Mosquito(二分+状态压缩+最大流)
- 游戏、脑洞大开1(密码简单破译)
- Log4net核心组成
- java.security.AccessControlException: access denie
- Net开发的部分知名网站案例
- leetcode题解279-完全平方数
- MYSQL主从复制操作文档
- php打印当前页面隐藏页眉,javascript实现window.print()去除页眉页脚_javascript技巧
- 单片机控制步进电机程序c语言正反转停止,51单片机控制步进电机的启动、停止、正转、反转...
- 100个必会的python脚本-python100例,python经典例题
- Python 结合bat批处理文件 实现密码保管箱
- Exploring Visual Relationship for Image Captioning
- matlab中zeros和ones函数使用方法
- OneNote如何同步到OneDrive,并且如何在PC和手机上完美使用OneNote
- 阿里云认证有什么用?考试费用高不高?
- 云计算工程师必备技能
- 第四周项目三计算并联电阻
热门文章
- EOS区块链浏览器:scaneos.io | EOS必备工具 |什么是区块链浏览器 | 怎么使用区块链浏览器
- 4个图片无损压缩工具,快速批量压缩图片,图片高清不糊
- 广告中的DSP、SSP和ADX
- Ubuntu 添加root用户
- 第二章 基本Bean的装载
- linux sd卡驱动流程图,SD卡驱动(详细介绍,不明白的人可以仔细看看了.有流程图)-转-OpenEdv-开源电子网...
- 循环神经网络RNN论文解读
- 汇编指令学习(CMP,TEST)
- c2-00支持java_双卡超长待机 经典实用诺基亚C2-00图赏
- 自动驾驶-自适应卡尔曼滤波AKF