Interrupt
Interrupt ,给线程发送一个中断信号,如给t1线程发送中断信号,t1.interrupt();
isInterrupted() 检测线程的中断信号状态 ,返回true的条件是线程是alive的,线程被中断了。如果线程已经结束了not alive,则返回false
wait和sleep和join这三个方法都会抛出InterruptException检查异常
/**
*
* 在t1线程里没有sleep,join,wait方法时,给t1线程发送中断信号, t1.interrupt();
* t1线程的中断状态由false变为true了
*
*/
public class Demo11 { public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(() -> {
System.out.println("t1 is running");
while (true) { }
});
t1.start(); try {
Thread.sleep(1_000);
System.out.println("before "+t1.isInterrupted());
t1.interrupt();
System.out.println("after "+t1.isInterrupted());
} catch (InterruptedException e1) {
e1.printStackTrace();
} }
}
/**
*
* 在t1线程里没有sleep,join,wait方法时,给t1线程发送中断信号, t1.interrupt();
* t1线程的中断状态由false变为true了
* 在main线程用t1.isInterrupted()检测t1线程的中断状态,
* main线程sleep一会儿,t1线程这时候break跳出循环了,t1线程结束了,那么用t1.isInterrupted()检查的结果一定是false
* main线程去掉sleep,相当于给线程t1发送中断信号后,立即使用t1.isInterrupted()检查状态,结果一定是true
*
*/
public class Demo12 { public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(() -> {
System.out.println("t1 is running");
while (true) {
// System.out.println("#######");
//如果检测到中断信号变为true了,跳出循环,t1线程变为ternated了,t1线程结束了
if(Thread.currentThread().isInterrupted()) {
break;
}
}
});
t1.start(); try {
Thread.sleep(1_000);
System.out.println("before "+t1.isInterrupted()); //false
t1.interrupt();
// Thread.sleep(100);
System.out.println("after "+t1.isInterrupted() + ", alive = " + t1.isAlive()); //false
} catch (InterruptedException e1) {
e1.printStackTrace();
} }
}
当线程中使用了wait方法
/**
*
*当线程中有sleep,wait,join等方法时,采用 t.interrupt();的方式打断线程,因为这些方法sleep,wait,join
*会捕获检查异常InterruptedException,并且捕获异常时候,中断信号被clear,即isInterrupted()返回false,
*
*
*/
public class Demo13 { private static final Object MONITOR = new Object(); public static void main(String[] args) { Thread t = new Thread() {
@Override
public void run() {
while (true) {
synchronized (MONITOR) {
System.out.println("=========>Before");
try {
MONITOR.wait();
System.out.println("=========>Wait");
} catch (InterruptedException e) {
System.out.println("###"+isInterrupted());
e.printStackTrace();
}
System.out.println("=========>After");
}
}
}
};
t.start(); try {
Thread.sleep(1_000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
t.interrupt();
try {
Thread.sleep();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("^^^"+t.isInterrupted()); }
}
当线程中使用sleep方法
/**
*
* 捕获InterruptedException后,会将中断信号状态clear,即中断信号为false
*
*/
public class Demo14 { public static void main(String[] args) {
Thread t = new Thread() {
@Override
public void run() {
while (true) {
try {
Thread.sleep();
} catch (InterruptedException e) {
System.out.println("###" + isInterrupted());
e.printStackTrace();
}
}
}
};
t.start(); try {
Thread.sleep(1_000);
} catch (InterruptedException e1) {
e1.printStackTrace();
} t.interrupt();
//这里休眠一段时间是为了让t线程在捕获异常后,将中断信号状态clear掉,即t.isInterrupted()会返回false
try {
Thread.sleep();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("^^^" + t.isInterrupted() + " alive : " + t.isAlive());
}
}
当线程中使用join方法
/**
*
* t.join();抛出InterruptedException的条件
* 当前线程是main,如果打断了main线程,t.join(),方法才抛出异常 ,同样main线程 的中断信号状态也被clear了,
*/
public class Demo15 {
public static void main(String[] args) { Thread t = new Thread() {
@Override
public void run() {
while (true) { }
}
};
t.start(); // 获取当前线程
Thread main = Thread.currentThread();
Thread t2 = new Thread() {
@Override
public void run() {
try {
Thread.sleep();
} catch (InterruptedException e) {
e.printStackTrace();
}
main.interrupt();
System.out.println("打断main线程了");
}
};
t2.start(); try {
// 这段代码是指t线程执行完后,再继续执行main线程,如果这时候打断main线程,那么就会抛出异常
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
Interrupt的更多相关文章
- wait、notify、sleep、interrupt对比分析
对比分析Java中的各个线程相关的wait().notify().sleep().interrupt()方法 方法简述 Thread类 sleep:暂停当前正在执行的线程:(类方法) yield:暂停 ...
- 【SPI】Polling Interrupt DMA
三種將資料在I/O間傳送的方法有 1. Polling2. Interrupt-driven I/O3. DMA(Direct Memory Access) Polling:最簡單的方式讓I/O de ...
- PIC32MZ tutorial -- OC Interrupt
In my previous blog "PIC32MZ tutorial -- Output Compare", I shows how to apply Output Comp ...
- PIC32MZ tutorial -- External Interrupt
In my older blog "PIC32MZ tutorial -- Key Debounce", I shows how to acheive key debounce w ...
- Java多线程系列--“基础篇”09之 interrupt()和线程终止方式
概要 本章,会对线程的interrupt()中断和终止方式进行介绍.涉及到的内容包括:1. interrupt()说明2. 终止线程的方式2.1 终止处于“阻塞状态”的线程2.2 终止处于“运行状态” ...
- java多线程系类:基础篇:09之interrupt()和线程终止方式
概要 本章,会对线程的interrupt()中断和终止方式进行介绍.涉及到的内容包括:1. interrupt()说明2. 终止线程的方式2.1 终止处于"阻塞状态"的线程2.2 ...
- 51单片机中断interrupt……using……
51单片机中断细节的一些问题. interrupt0:外部中断0interrupt1:定时器中断0interrupt2:外部中断interrupt3:定时器中断1interrupt4:串口 using ...
- STM8L --- External interrupt
note 1: Several interrupts can be pending at the same time. When an interrupt request is not servic ...
- Java并发包源码学习之AQS框架(三)LockSupport和interrupt
接着上一篇文章今天我们来介绍下LockSupport和Java中线程的中断(interrupt). 其实除了LockSupport,Java之初就有Object对象的wait和notify方法可以实现 ...
- JAVA thread0.interrupt()方法
interrupt()只是改变中断状态而已,interrupt()不会中断一个正在运行的线程.这一方法实际上完成的是,给受阻塞的线程抛出一个中断信号,这样受阻线程就得以退出阻塞的状态. 更确切的说,如 ...
随机推荐
- mybatis_02简单操作数据库
模糊查询用户信息 <!-- [${}]:表示拼接SQL字符串 [${value}]:表示要拼接的是简单类型参数. 注意: 1.如果参数为简单类型时,${}里面的参数名称必须为value 2.${ ...
- mediainfo使用
1.linux安装mediainfo yum install mediainfo(epel源安装) 2.输出文件信息到xml文件 mediainfo --OUTPUT=XML -f ftp ...
- viewer.js 视图预览demo
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- CSS笔记1:属性--定位
相对定位是“相对于”元素在文档中的初始位置,而绝对定位是“相对于”最近的已定位祖先元素,如果不存在已定位的祖先元素,那么“相对于”最初的包含块. 元素定位 属性 版本 继承 描述 position c ...
- es6 语法 (set 和 map)
{ let list = new Set(); list.add(5); list.add(7); console.log('size', list, list.size); //{5, 7} 2 } ...
- JavaScript面向对象编程指南(三) 函数
第3章 函数 3.1 什么是函数 函数:本质是一种代码的分组形式.函数的声明如下: <script type="text/javascript"> /*函数的声明组成: ...
- error 2593 operator << 不明确的可能的解决方法
编译Martinez算法时遇到该问题,提示重载的<<操作符调用不明确. 解决方法为:在cpp文件中将重载的该操作符的实现前添加完整的命名空间路径.
- 30.Odoo产品分析 (四) – 工具板块(2) – 搜索和仪表盘(2)
查看Odoo产品分析系列--目录 在前面的模块中,简单介绍过了odoo如何搜索系统中的各种数据集,并保存这些过滤器,以便在之后需要时能够轻松访问这些过滤器.这里将做更详细的介绍.最后分析仪表盘的功能, ...
- Linux应用和系统库的2种安装方式---源码安装tarball和二进制rpm包
一.应用程序和系统库从哪里来? 两种机制,源码安装和二进制安装. 二.源码安装 tarball 1.核心思想是:利用开源代码,自己编译生成应用程序或者库,要求系统上必须已安装TMG(tar, make ...
- Spark应用【根据新df更新旧df】
// 主键字段保持不变,再转换回来 var columnMap:Map[String, String] = Map() for(key <- keysOpt){ columnMap += (ke ...