当前位置: 首页 > news >正文

【面经】interrupt()、interrupted()和isInterrupted()的区别与使用

       📝个人主页:五敷有你      

 🔥系列专栏:面经

⛺️稳中求进,晒太阳

interrupt方法

        如果打断线程正在sleep,wait,join会导致被打断的线程抛出InterruptedException,并清除打断标记。如果打断正在运行的线程,则会设置打断标记。park的线程被打断也会被设置打断标记

Interrupted方法

Thread类的静态方法。检测当前线程的中断标记,返回一个boolean并清除中断状态,其连续两次调用的返回结果不一样,因为第二次调用的时候,线程的中断状态已经被清除了,会返回一个false.

isInterrupted()方法

测试线程是否被打断,不会清除中断状态。

演示代码

场景:在sleep时打断

package jvm;import lombok.extern.slf4j.Slf4j;import java.util.logging.Logger;public class TestInterrupt {public static void main(String[] args) throws InterruptedException {MyThread t1=new MyThread();t1.start();Thread.sleep(10000);t1.interrupt();}}
class MyThread extends Thread{@Overridepublic void run() {while (!this.isInterrupted()){try {System.out.println("执行1s");Thread.sleep(1000);} catch (InterruptedException e) {System.out.println("在睡觉的时候被打断了");throw new RuntimeException(e);}}}
}

如上:导致被打断的线程抛出InterruptedException,并清除打断标记。

场景:在正常运行时,打断标记的情况

package jvm;import lombok.extern.slf4j.Slf4j;import java.util.logging.Logger;public class TestInterrupt {public static void main(String[] args) throws InterruptedException {MyThread t1=new MyThread();t1.start();Thread.sleep(10000);t1.interrupt();}}
class MyThread extends Thread {@Overridepublic void run() {while (!this.isInterrupted()) {System.out.println("执行1s");System.out.println("标记" + this.isInterrupted());}}
}

如上:正常运行时被打断是不会被清除标记的。

场景:使用park暂停的线程 打断标记情况

package jvm;import lombok.extern.slf4j.Slf4j;import java.util.concurrent.locks.LockSupport;
import java.util.logging.Logger;public class TestInterrupt {public static void main(String[] args) throws InterruptedException {MyThread t1=new MyThread();t1.start();Thread.sleep(10000);t1.interrupt();}}
class MyThread extends Thread {@Overridepublic void run() {while (!this.isInterrupted()) {LockSupport.park();System.out.println("标记" + this.isInterrupted());}}
}

如上:park被暂停的线程被打断是不会清除标记的。

场景:测试Thread.interrupted静态方法

package jvm;import lombok.extern.slf4j.Slf4j;import java.util.concurrent.locks.LockSupport;
import java.util.logging.Logger;public class TestInterrupt {public static void main(String[] args) throws InterruptedException {MyThread t1=new MyThread();t1.start();Thread.sleep(10000);t1.interrupt();}}
class MyThread extends Thread {@Overridepublic void run() {while (true){boolean interrupted = Thread.interrupted();System.out.println("标记:"+interrupted);if(interrupted){break;}}}
}

http://www.lryc.cn/news/332809.html

相关文章:

  • 了解这些技术:Flutter应用顺利登陆iOS平台的步骤与方法
  • 经济学 劳动市场 医疗经济学
  • vue + koa + Sequelize + 阿里云部署 + 宝塔:宝塔数据库连接
  • 华为昇腾认证考试内容有哪些
  • Spring Boot接收从前端传过来的数据常用方式以及处理的技巧
  • EFCore通用数据操作类
  • java Web 辅助学习管理系统idea开发mysql数据库web结构java编程计算机网页源码maven项目
  • 使用Python实现K近邻算法
  • Celery的任务流
  • 使用Arcpy进行数据批处理-批量裁剪
  • 【攻防世界】ics-05
  • VTK的交互器
  • ChatGPT(3.5版本)开放无需注册:算力背后的数据之战悄然打响
  • python项目练习——14.学生管理系统
  • 基于SpringBoot的公益慈善平台
  • Python网络爬虫(一):HTML/CSS/JavaScript介绍
  • 机器学习每周挑战——旅游景点数据分析
  • 开发语言漫谈-C语言
  • vue3导入excel并解析excel数据渲染到表格中,纯前端实现。
  • Java常用API之Encoders类解读
  • java中大型医院HIS系统源码 Angular+Nginx+SpringBoot云HIS运维平台源码
  • windows部署Jenkins并远程部署tomcat
  • 设计模式|责任链模式(Chain of Responsibility Pattern)
  • 文件服务器之二:SAMBA服务器
  • 20.安全性测试与评估
  • 阿里巴巴实习面经
  • javaweb学习(day11-监听器Listener过滤器Filter)
  • 教你快速认识Java中的抽象类和接口
  • Linux第5课 Linux目录介绍
  • GitHub要求2FA?不慌,有它(神锁离线版)帮你!