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()不会中断一个正在运行的线程.这一方法实际上完成的是,给受阻塞的线程抛出一个中断信号,这样受阻线程就得以退出阻塞的状态. 更确切的说,如 ...
随机推荐
- Android开发过程中的坑及解决方法收录(一)
之前使用了Android Studio的插件直接为button绑定了监听器,并实现onClick方法(我的onClick方法无论点击哪一个都是要实现setcontentview这个方法设置layout ...
- 异常:Data = 由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值。
做项目的时候,将DataTable序列化成Json,通过ashx向前台返回数据的时候,前台总是获取不到数据,但是程序运行却没问题, 没抛出异常.一时找不到办法,减小输出的数据量,这时前台可以接收到页面 ...
- 课程作业——Python基础之使用turtle库画出红旗
代码如下: import turtle # 设置画笔和背景颜色 turtle.color('yellow') turtle.bgcolor('red') # 通过偏移量和尺寸大小画星星 def dra ...
- laravel表单验证
效果展示: 代码 实现: 后台: use Validator; public function login() { if($input = Input::all()){ //验证提交的 ...
- 玩游戏 学Flex布局
大家好,今天推荐一个学习Flex布局的网页小游戏,非常不错,是国外的牛人开发的,值得一试,重复几次,就会大概了解Flex的属性了! 地址: http://flexboxfroggy.com/#zh-c ...
- 05-HTML-超链接标签
<html> <head> <title>超链接标签学习</title> <meta charset="utf-8"/&g ...
- @RequestBody Spring MVC 示例
1.前端的访问请求 <script type="text/javascript"> $(document).ready(function(){ var saveData ...
- 一个JVM进程启动后里面有几个线程
在写Java程序时,通常我们管只有一个main函数(而没有别的Thread或Runnable的程序)叫单线程程序.但是我们写的这个所谓的单线程程序只是JVM这个程序中的一个线程,JVM本身是一个多线程 ...
- Angular的12个经典问题,看看你能答对几个?(文末附带Angular测试)
Angular作为目前最为流行的前端框架,受到了前端开发者的普遍欢迎.不论是初学Angular的新手,还是有一定Angular开发经验的开发者,了解本文中的12个经典面试问题,都将会是一个深入了解和学 ...
- 第一次使用VS Code时你应该知道的一切配置
前言 本文最新内容将在GitHub上实时更新. VS Code 本来是前端人员专用,但由于它实在是太好用了,于是,各种开发方向的码农也正在用 VS Code 作为他们的主力编程工具.甚至是一些写作的同 ...