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()不会中断一个正在运行的线程.这一方法实际上完成的是,给受阻塞的线程抛出一个中断信号,这样受阻线程就得以退出阻塞的状态. 更确切的说,如 ...
随机推荐
- SpringMVC 的运行原理
0. 灵魂的拷问 问:SpringMVC 是什么?它有什么作用? 答:SpringMVC 的全称是 Spring Web Model-View-Controller,它是 Spring Fram ...
- 今天给大家补充一下 background 用法
补充一个知识点 1,浏览器默认字体大小是font-size:16px:谷歌最小字体是10px,其他浏览器是12px 2. 选择器 通配符选择器 * 表示 3.background 背景 ...
- Javascript继承2:创建即继承----构造函数继承
//声明父类 function SuperClass(id){ //值类型公有属性 this.id = id; //引用类型公有属性 this.books = ['Html','Css']; } // ...
- K8S Calico
NetworkPolicy是kubernetes对pod的隔离手段,是宿主机上的一系列iptables规则. Egress 表示出站流量,就是pod作为客户端访问外部服务,pod地址作为源地址.策略可 ...
- loj#6436. 「PKUSC2018」神仙的游戏(生成函数)
题意 链接 Sol 生成函数题都好神仙啊qwq 我们考虑枚举一个长度\(len\).有一个结论是如果我们按\(N - len\)的余数分类,若同一组内的全为\(0\)或全为\(1\)(?不算),那么存 ...
- cf997C. Sky Full of Stars(组合数 容斥)
题意 题目链接 \(n \times n\)的网格,用三种颜色染色,问最后有一行/一列全都为同一种颜色的方案数 Sol Orz fjzzq 最后答案是这个 \[3^{n^2} - (3^n - 3)^ ...
- Java 线程方法
线程标识相关 方法 描述 public Thread(Runnable target, String name) 带参数的构造方法, 第二个参数是线程名称 public static Thread ...
- Visual Studio 20周年,我和VS不得不说的故事
Visual Studio 2017正式版已如期发布(点击这里查看发布全记录)!自去年 11 月正式宣布 Visual Studio 项目之后,微软终于正式推出了“宇宙最强集成开发环境(IDE)”的最 ...
- 浅谈Kotlin(一):简介及Android Studio中配置
浅谈Kotlin(一):简介及Android Studio中配置 浅谈Kotlin(二):基本类型.基本语法.代码风格 浅谈Kotlin(三):类 浅谈Kotlin(四):控制流 前言: 今日新闻:谷 ...
- Android 进度条按钮实现(ProgressButton)
有些App在点击下载按钮的时候,可以在按钮上显示进度,我们可以通过继承原生Button,重写onDraw来实现带进度条的按钮. Github:https://github.com/imcloudflo ...