理解高并发(10).线程通信之_线程中断技术

来源:互联网 发布:黑马java基础班测试题 编辑:程序博客网 时间:2024/06/05 07:46
对于线程中断,需要注意的几点:
  • interrupt() 让阻塞的线程中断处理
try{
Thread.sleep(3000L);
}catch(InterruptedException e){
/*************************
运行interrupt()后会抛出InterruptedException 异常
如果希望中断处理的话,需要做如下操作:
1. 抛出异常
2. 在异常代码中执行 Thread.currentThread().interrupt();
3. 或在异常代码中执行 interrupt();
****************************/
}

  • interrupt() 让当前线程中断处理
  • interrupted() 检查当前线程是否中断, 并清除中断
  • isInterrupted() 查看当前线程的中断状态,true-中断; false-未中断
原创粉丝点击