java线程是协作式,而非抢占式

调用一个线程的interrupt() 方法中断一个线程,并不是强行关闭这个线程,只是跟这个线程打个招呼,将线程的中断标志位置为true,线程是否中断,由线程本身决定。

isInterrupted() 判定当前线程是否处于中断状态。

static方法interrupted() 判定当前线程是否处于中断状态,同时中断标志位改为false。

方法里如果抛出InterruptedException,线程的中断标志位会被复位成false,如果确实是需要中断线程,要求我们自己在catch语句块里再次调用interrupt()。

定义一个匿名类,继承了Thread类,定义一个构造函数,把名字传递进去,定义一个新的线程之后,在主线程中停止20毫秒,把线程中止掉,然后while循环里面调用isInterrupted判断是否中止了线程,然后中止线程之后把线程执行完毕,请看图

interrupt和isInterrupted的基本使用方法的更多相关文章

  1. interrupt interrupted isInterrupted 方法对比、区别与联系 多线程中篇(八)

    interrupt interrupted isInterrupted 是三个“长相”非常类似的方法. 本文将对这三个方法简单的对比下,首先了解下线程停止的方式 线程停止方式 在Java中如果想停止一 ...

  2. 多线程-interrupt(),isInterrupted(),interrupted()(转)

    Content 背景 中断 相关方法 阻塞方法 不可中断的阻塞方法 处理不支持中断的线程中断的常用方法 处理InterruptedException 待决中断 实例1 实例2 参考资料 Top 背景 ...

  3. 多线程-interrupt(),isInterrupted(),interrupted()

    背景 由于使用stop方法停止线程非常暴力,可能会导致一系列问题.因此,提出一种温和的方式:请求另外一个先不要在执行了,这就是中断方式. 此外有这样的场景:编写 一个程序,需要暂停一段时间,于是调用T ...

  4. java多线程 interrupt(), interrupted(), isInterrupted()方法区别

    interrupt()方法: 作用是中断线程. 本线程中断自身是被允许的,且"中断标记"设置为true 其它线程调用本线程的interrupt()方法时,会通过checkAcces ...

  5. Thread的中断机制(interrupt),循环线程停止的方法

    一.中断原理 中断线程 线程的thread.interrupt()方法是中断线程,将会设置该线程的中断状态位,即设置为true,中断的结果线程是死亡.还是等待新的任务或是继续运行至下一步,就取决于这个 ...

  6. 十四 关于interrupt, interrupted, isInterrupted

    1 判断线程是否是停止状态? interrupt() : interrupt方法用于中断线程.调用该方法的线程的状态为将被置为"中断"状态. 注意:线程中断仅仅是置线程的中断状态位 ...

  7. interrupt ,interrupted 和 isInterrupted

    1.interrupt  interrupt方法用于中断线程.调用该方法的线程的状态为将被置为"中断"状态. 注意:线程中断仅仅是置线程的中断状态位,不会停止线程.需要用户自己去监 ...

  8. Thread interrupt方法解析

    初步理解 我们在看一些多线程代码的时候,有的时候会碰到使用interrupt()方法的时候.从字面的意思来理解,应该就是中断当前正在执行的线程.那么,对于一个我们设计的普通线程来说,如果我们在主线程里 ...

  9. 关于Thread类中三个interrupt方法的研究与学习(转)

    先看三个方法原型: public void interrupt(): public boolean isInterrupted(): public static boolean interrupted ...

随机推荐

  1. Scrum立会报告+燃尽图 07

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2289] 版本控制:https://git.coding.net/liuyy08 ...

  2. 在pixi中使用你的自定义着色器

    通过几天的学习,对openGL.shader有了一个大致的了解. 回到学习的初衷吧,在基于pixi.js重构D3项目的时候,因为精灵层级的问题,我得按照一定的先后顺序将不同类别的精灵添加到场景中去. ...

  3. Loj 2005 相关分析

    Loj 2005 相关分析 大力把式子拆开. \[ \begin{aligned} a &= \frac {\sum_{i=L}^{R} (x_i-\bar{x})(y_i-\bar{y})} ...

  4. android logger的使用

    1. 进入GitHub页面 https://github.com/orhanobut/logger 2. 在gradle里增加 compile 'com.orhanobut:logger:1.15' ...

  5. 1106-冒泡算法C程序(语法树)

    #include <stdio.h> main() { int i,j,temp; int a[10]; for(i=0;i<10;i++) scanf ("%d,&quo ...

  6. bzoj 4570: [Scoi2016]妖怪 凸包

    题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=4570 题解 我们知道如果一个怪物要取到攻击力的最大值,那么一定是把防御力都转化了 所以我 ...

  7. 自动化工具 Python 调 Jmeter 执行参数化 jmx 脚本

    烦透了jmeter输入如下鬼命令: Jmeter -n -t {tmpjmxfile} -l {csvfilename} -e -o {htmlreportpath} 尤其是{htmlreportpa ...

  8. 一个filebeat实例 设置多topic设置

    方法1:一实例多topic: https://discuss.elastic.co/t/filebeat-5-0-output-to-kafka-multiple-topics/67934 The d ...

  9. panabit web认证界面个性化

    原理:利用js代码在前端页面进行个性化 1.禁止信息提示 url:http://192.168.0.246:8010/restrict.htm 文件:restrict.png  restrict.ht ...

  10. Mysql8.0 Public Key Retrieval is not allow错误的解决办法

    在使用Mysql 8.0时重启后启动项目的事后会报错com.mysql.jdbc.exceptions.jdbc4.MysqlNonTransientConnectionException: Publ ...