首页  

java线程中断机制     所属分类 java 浏览量 1116
中断标志位
使用 interrupt() 方法向目标线程发送中断请求
Thread  isInterrupted() 用来获取中断标记值 是否被中断

public boolean isInterrupted()   ClearInterrupted 默认为false

private native boolean isInterrupted(boolean ClearInterrupted)

中断响应处理
不响应 不做任何处理 InputStream read 内部锁等不会响应中断 
 中断标志位为true
抛出 InterruptedException 



public static native void sleep(long millis) throws InterruptedException

Object wait 
Thread sleep join 
BlockingQueue take put 
Lock  
void lockInterruptibly() throws InterruptedException
CountDownLatch await
CyclicBarrier await

        try{
			Thread.sleep(time);
		}catch(Throwable e){
			//  保留中断标志
			Thread.currentThread().interrupt();
		}
		
		
抛出 InterruptedException 的方法 再抛出该异常前 把线程中断标志位重置为false

抛出InterruptedException并唤醒线程

结合中断和自定义状态来终止线程

上一篇     下一篇
极简主义生活

乔布斯语录:活着就是为了改变世界

ScheduledThreadPoolExecutor使用介绍

职场十六金句

西游记团队成功秘诀

一句话告诉你什么是互联网+