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()不会中断一个正在运行的线程.这一方法实际上完成的是,给受阻塞的线程抛出一个中断信号,这样受阻线程就得以退出阻塞的状态. 更确切的说,如 ...
随机推荐
- 性能监控(4)–linux下的pidstat命令
pidstat是一个可以监控到线程的监控工具,可以使用-p指定进程ID. pidstat–p <PID> [delay] [times] –u –t 可以监控线程的CPU使用率 当某一个线 ...
- xml方式封装数据方法
1.xml方式封装数据方法 2.demo <?php xml方式封装数据方法 /** * [xmlEncode description] * @param [type] $code [descr ...
- C#时间、日期 的操作
原文地址:http://zhidao.baidu.com/link?url=q-0No1LWfdyqQKFBg8RN5h0K-X0VygSbFI4sR8lBPwDExkIIzXQivyMKqX7V1y ...
- 2017-11-28 中文编程语言之Z语言初尝试: ZLOGO 4
"中文编程"知乎专栏原文. 作者为本人. @TKT2016 开发的Z语言(ZLOGO是它的一个部分)是本人至今看到的唯一一个仍活跃开发的开源且比较完整的中文编程语言项目. 它的源码 ...
- iphone怎么投屏到电脑屏幕上
随着苹果手机的更显换代,苹果手机的功能越来越强大,其中iphone手机更新了airplay镜像功能,所以想要手机投屏电脑的小伙伴就更加方便了,但是iphone怎么投屏到电脑呢?大家不用着急,下面即将为 ...
- [性能调优]PeopleSoft Trace 分析工具 - TraceMagic
PeopleSoft Trace 文件包含大量的信息,在前面文章讲解过如何查看trace日志文件,这边文章介绍一个工具可以很好的分析trace日志文件. TraceMagic 是由oracle开发的一 ...
- 如何将web项目部署到weblogic
在Eclipse中配置weblogic11g服务器: 下载并安装Eclipse:www.eclipse.org 下载并安装Weblogic Server Plugin for Eclipse:http ...
- git 入门教程之协同开发
前面我们已经介绍过远程仓库的相关概念,不过那时并没有深入探讨,只是讲解了如何创建远程仓库以及推送最新工作成果到远程仓库,实际上远程仓库对于团队协同开发很重要,不仅仅是团队协同开发的基础,也是代码备份的 ...
- webstorm 2018 激活破解方法大全
转载自:https://blog.csdn.net/voke_/article/details/76418116 方法一:(更新时间:2018/4/8)v3.3 注册时,在打开的License Act ...
- RMAN restore fails with ORA-01180: can not create datafile 1
最近在验证.测试备份有效性时,遇到了"ORA-01180: can not create datafile 1"这个错误,顺便结合metalink的官方文档"RMAN ...