理解Java的Thread中的Interrupt机制
2017-03-24 12:02
302 查看
看了这个博客后,茅舍顿开的,http://m.blog.csdn.net/article/details?id=6941802
Java中线程安全的中断是使用interrupt(),
运行中的线程不会因为interrupt()而中断,因为它仅仅是一个信号(status)
等待中的线程(wait(long),sleep(long),join(long))收到中断信号会抛出InterruptedException
静态方法Thread.interrupted()会清除中断标记(ClearInterrupted status),
以及线程终结了调用isInterrupted()都会返回false。
Java中线程安全的中断是使用interrupt(),
运行中的线程不会因为interrupt()而中断,因为它仅仅是一个信号(status)
等待中的线程(wait(long),sleep(long),join(long))收到中断信号会抛出InterruptedException
静态方法Thread.interrupted()会清除中断标记(ClearInterrupted status),
以及线程终结了调用isInterrupted()都会返回false。
package cn.cherish.mboot; /** * @author Cherish * @version Id: TestInterrupt.java, v 1 2017/3/24 10:19 Cherish Exp $$ */ public class TestInterrupt { public static void main(String[] args) { Thread sleepThread = new Thread(() -> { try { System.out.println("【sleepThread】沉睡的线程" + Thread.currentThread().getName()); Thread.sleep(1000_00); } catch (InterruptedException e) { // e.printStackTrace(); System.out.println("【sleepThread】沉睡中收到中断信号,会抛出InterruptedException"); }finally { System.out.println("【sleepThread】finally方法可以做些啥"); } while(true){ System.out.println("【sleepThread】感谢中断信号,我不睡了"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }); Thread loopThread = new Thread(() -> { while (!Thread.currentThread().isInterrupted()) { System.out.println("【loopThread】 没收到中断信号,随便玩"); } int count = 0; while (Thread.currentThread().isInterrupted()) { ++count; System.out.println("【loopThread】 收到中断信号,改变操作方式,我可以选择释放资源,但是我不 " + count); if (count == 10) { boolean beforeState = Thread.interrupted(); System.out.println("【loopThread】 beforeState = " + beforeState); System.out.println("Thread.interrupted() 会重置中断标记为false"); boolean nowState = Thread.currentThread().isInterrupted(); System.out.println("【loopThread】 nowState = " + nowState); System.out.println("【loopThread】任性了10次,该结束了"); } } System.out.println("【loopThread】正式结束"); }); sleepThread.start(); loopThread.start(); try { Thread.sleep(1);//等待其它两个线程的执行 } catch (InterruptedException e) { e.printStackTrace(); } sleepThread.interrupt(); loopThread.interrupt(); while (true) { try { Thread.sleep(1000);//等待其它两个线程收到中断信号 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("【sleepThread】 isInterrupted = " + sleepThread.isInterrupted()); System.out.println("【loopThread】 isInterrupted = " + loopThread.isInterrupted()); } } }
相关文章推荐
- JAVA Thread的中断机制(interrupt)
- 理解java的interrupt机制
- Java事件机制理解及应用
- 用"堆栈区数据复制"理解Java赋值和参数传递?机制的心得
- 理解Java赋值和参数传递机制.docx
- Java线程同步机制synchronized关键字的理解
- java thread volatile 理解
- Java的Package与Import机制之我的理解(初学者的心得)
- 理解Java异常处理机制——Java异常处理的一个综合实例
- 深入理解JAVA事件机制
- 深入理解JAVA事件机制
- java垃圾回收机制的程序理解
- java异常处理机制的深入理解
- JAVA的事件机制理解
- 用"堆栈区数据复制"理解Java赋值和参数传递?机制的心得
- 理解Java ClassLoader机制(转自http://www.blogjava.net/pandawang/archive/2007/10/05/64639.html)
- 关于java异常处理机制的深入理解
- 理解Java ClassLoader机制
- 理解Java ClassLoader机制 (转载http://www.blogjava.net/pandawang/archive/2006/08/20/64639.html)
- 理解Java异常处理机制——Java异常处理的一个综合实例