文章目录

  • 二、如何安全地终止线程
    • interrupt()、isInterrupted()、interrupted()的区别与使用
      • interrupt()
      • isInterrupted()
      • interrupt==ed==()
      • 注意
    • 使用interrupt()方法终止线程
    • 外部干涉

二、如何安全地终止线程

有2种方法可以安全地终止线程:

  1. 使用interrupt()
  2. 外部干涉(推荐)。

interrupt()、isInterrupted()、interrupted()的区别与使用


interrupt()

注意区分interrupted()方法;

public void interrupt()

interrupt()是用于中断线程的,调用该方法,线程的状态将被置为"中断"状态。
注意:线程中断仅仅是设置线程的中断状态位,++不会停止线程++。需要用户自己去监视线程的状态为并做处理。


isInterrupted()

isInterrupted()测试线程是否已经中断。线程的 中断状态 不受该方法的影响。

public boolean isInterrupted()

interrupted()

public static boolean interrupted()

interrupted()第一次使用返回true,并清除中断标志位,在此之后查询中断状态isInterrupt()都会返回false,第一次返回的true可以跳出循环。第二次以及以后都是返回false。


区别案例

public class InterruptExample {private static class MyThread extends Thread {@Overridepublic void run() {while (true) {System.out.println(isInterrupted());//interrupt()并不会中断线程,所以后面的代码会被继续执行;interrupt();System.out.println(isInterrupted());//interrupted()第一次返回true,并清除标志位,所以以后查询中断状态返回的都是falseSystem.out.println(interrupted());System.out.println(isInterrupted()); //falsebreak;}System.out.println("Thread end");}}public static void main(String[] args){Thread thread = new MyThread2();thread.start();}
}

输出

false
true
true   //interrupted()第一次返回true
false
Thread end

注意

  1. 通过调用一个线程的 interrupt()来中断该线程,如果该线程处于阻塞、限期等待或者无限期等待状态,那么就会抛出 InterruptedException,从而提前结束该线程。但是不能中断 I/O 阻塞和 synchronized 锁阻塞。

  2. suspend()、resume()和stop()过期了,不建议使用;

public class InterruptExample {private static class MyThread1 extends Thread {@Overridepublic void run() {try {Thread.sleep(2000);System.out.println("Thread run");} catch (InterruptedException e) {e.printStackTrace();}}}
}
public static void main(String[] args) throws InterruptedException {Thread thread1 = new MyThread1();thread1.start();thread1.interrupt();System.out.println("Main run");
}

由于线程中调用了Thread.sleep()方法,因此会抛出一个 InterruptedException,从而提前结束线程,不执行之后的语句。

Main run
java.lang.InterruptedException: sleep interruptedat java.lang.Thread.sleep(Native Method)at InterruptExample.lambda$main$0(InterruptExample.java:5)at InterruptExample$$Lambda$1/713338599.run(Unknown Source)at java.lang.Thread.run(Thread.java:745)

使用interrupt()方法终止线程

public class InterruptExample {private static class MyThread2 extends Thread {@Overridepublic void run() {//while (!isInterrupted()) {// ..}System.out.println("Thread end");}}
}
public static void main(String[] args) throws InterruptedException {Thread thread2 = new MyThread2();thread2.start();thread2.interrupt();
}
Thread end

外部干涉

public class InterruptExample {private static class MyThread extends Thread {private volatile boolean on = true;@Overridepublic void run() {while (on) {//todo}}public void cancel(){on = false;}}public static void main(String[] args) {Thread thread = new MyThread();thread.start();thread.cancel();}
}

如何安全地终止线程interrupt()、isInterrupted()、interrupted()的区别与使用相关推荐

  1. 线程中断的三个方法的区别(interrupt/isInterrupted/interrupted)

    线程中断的三个方法的区别(interrupt/isInterrupted/interrupted)-总结自Java编程之美 方法定义 void interrupt():中断调用该方法的实例线程对象.当 ...

  2. java并发编程之正确地终止一个线程interrupt/interrupted

    以下demo是错误的终止线程的demo(使用thread.stop()方法实现终止线程): public class ErrorStopThreadDemo {public static void m ...

  3. java线程 stop()_java 多线程5: java 终止线程及中断机制 (stop()、interrupt() 、interrupted()、isInterrupted())...

    JAVA中有3种方式可以终止正在运行的线程 ①线程正常退出,即run()方法执行完毕了 ②使用Thread类中的stop()方法强行终止线程.但stop()方法已经过期了,不推荐使用 ③使用中断机制i ...

  4. 怎么查看线程的状态及interrupt优雅的关闭线程和interrupt()、interrupted()、isInterrupted()的作用以及区别在哪?

    怎么查看线程状态 jps指令查看我当前的进程ID jstack 线程ID 示例: public class StatusDemo {public static void main(String[] a ...

  5. 线程中断标志位 interrupt()、interrupted()、isInterrupted() 的认识

    常见问题 首先你是怎么去关闭一个开启的线程.调用中断方法之后,线程就立即停止运行吗? 带着这两个问题探讨一下,主要围绕着这三个方法讲述: interrupt().interrupted().isInt ...

  6. sleep interrupted异常_Java高并发3中断线程以及isInterrupted与interrupted区别

    一.复习上一节内容 wait()方法.中断正在运行的线程会抛出java.lang.InterruptedException.当线程调用共享对象的wait()方法时,当前线程只会释放当前共享变量的锁,不 ...

  7. interrupt、interrupted 、isInterrupted 区别

    interrupt: 调用方法,是线程处于中断状态,但是这个方法只是让线程设置为中断状态,并不会真正的停止线程.支持线程中断的方法就是在坚持线程中断状态,一旦线程中断状态被设置为中断,就会抛出异常. ...

  8. 多线程之 interrupt,interrupted,isInterrupted 方法区别

    首先看测试代码 /** * Created by Jarvis.y on 2020/11/5 * <p> * interrupt , interrupted , isInterrupted ...

  9. 简述Thread的interrupt()、interrupted()及isInterrupted()的区别

    前言 在java Thread类中,我们会看到interrupt().interrupted()及isInterrupted(),在大多数情况下,我们都不会使用到它们,但是有一个Interrupted ...

  10. isInterrupted、interrupt和interrupted

    由易到难开始介绍吧 isInterrupted 最容易理解的,查看线程是否被打断,没有副作用.可以自己询问自己,也可以在A线程中询问B线程. Thread A = new Thread(() -> ...

最新文章

  1. 音频数据建模全流程代码示例:通过讲话人的声音进行年龄预测
  2. matlab中simple是什么函数,[求助]Matlab2016b里没有simple函数
  3. 架构畅想:如果以你所会去进行架构,会到哪一步?
  4. SDUT - Mountain Subsequences(dp)
  5. 1小时打造HaaS版小小蛮驴智能车
  6. S3C2440 LCD驱动(FrameBuffer)实例开发一(转)
  7. 【转】关于测试方面的一些文章
  8. 保护眼睛颜色的RGB
  9. Android用悬浮按钮实现翻页效果
  10. 兔子繁殖问题(递归解决)
  11. 篮球图片html页面代码,教你用PS制作一个非常逼真的篮球图片
  12. java opengl 图片文字_如何通过opengl显示相机预览
  13. 【韩顺平】设计模式七大原则
  14. java.exe点击无反应_win7系统双击JeR安装包没有任何反应的解决方法
  15. [重装系统] windows 10 重装系统记录
  16. 盘点系列:一度大热的TWS耳机今年表现如何?
  17. 物联网资产暴露情况分析
  18. dl4j+fnlp关联度TopN
  19. (笔记)MLDN魔乐科技--五子棋
  20. SAP中工艺路线物料分配的生效日期问题处理案例

热门文章

  1. mysql体系结构内存_mysql 内存体系结构--session
  2. 200. 岛屿数量 leetcode
  3. 首届电子商务AI算法大赛 Organized by automlai
  4. git checkout切换分支
  5. android记事本的app,自己编写的Android记事本APP软件
  6. 电脑太慢了最简单的办法怎么弄_最简单的电脑端微信多开方法
  7. latex 目录层次设置
  8. Ribbon和Feign的对比-带简易例子
  9. 【2019南昌邀请赛网络赛 J】Distance on the tree【边权树剖+主席树】
  10. ConcurrentHashMap源码分析(保姆式讲解):Put、扩容原理详解 博主可答疑