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()不会中断一个正在运行的线程.这一方法实际上完成的是,给受阻塞的线程抛出一个中断信号,这样受阻线程就得以退出阻塞的状态. 更确切的说,如 ...
随机推荐
- Linux-cut命令(22)
cut剪切命令cut命令通常用来对某个文本文件进行解析,擅长处理以一个字符间隔的文本内容 -b :以字节(bytes)为单位进行分割.这些字节位置将忽略多字节字符边界,除非也指定了 -n 标志. -c ...
- JSJ—案例谈面向对象
有人告诉我那里遍地都是对象——我们把所有的程序代码放在main()里面,事实上,那根本就不是面向对象的做法,在Java的面向对象中,我们也会看到类和对象的不同,以及对象是如何让你的生活更美好(至少程序 ...
- WPF实现主题更换的简单DEMO
WPF实现主题更换的简单DEMO 实现主题更换功能主要是三个知识点: 动态资源 ( DynamicResource ) INotifyPropertyChanged 接口 界面元素与数据模型的绑定 ( ...
- hive drop 报错
执行 drop database xxxx cascade; 删除数据库的时候报错. 报错信息:Execution Error, return code 1 from org.apache.hadoo ...
- POJ2484
A Funny Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6178 Accepted: 3861 Desc ...
- laravel表单验证
效果展示: 代码 实现: 后台: use Validator; public function login() { if($input = Input::all()){ //验证提交的 ...
- thinkphp3.2.3模板渲染支持三元表达式
thinkphp3.2.3模板渲染支持三元表达式 {$status?'正常':'错误'} {$info['status']?$info['msg']:$info['error']} 注意:三元运算符中 ...
- es6 语法 (Decorator)
修饰器是一个函数,用来修改类的行为(注意:1.函数 2.修改行为 3.对类进行操作) { //修饰器函数定义 target:类本身,name名称,descriptor描述 let readonly ...
- 洛谷P1792 [国家集训队]种树(链表 贪心)
题意 题目链接 Sol 最直观的做法是wqs二分+dp.然而还有一种神仙贪心做法. 不难想到我们可以按权值从大到小依次贪心,把左右两边的打上标记,但这显然是错的比如\(1\ 231\ 233\ 232 ...
- Android 自定义AlertDialog的实现
Android默认的AlertDialog太单调,我们可以通过继承原生的Dialog来实现自定义的Dialog. 本文的自定义Dialog和原生的AlertDialog的创建方式类似,通过一个静态Bu ...