线程阻塞时,他通常被挂起,处于某种阻塞状态(BLOCKED , WAITING , TIMED_WAITING)
BlockingQueue 的put 和 take方法会抛出InterruptedException 和 Thread.sleep一样。当一个方法抛出InterruptedExceprion 时表示该方法时一个阻塞方法。
处理InterruptedException
- 传递给调用者
- 恢复中断,当代码是Runnable 的一部分时(implement Runnable),必须捕获异常
public class TaskRun implement Runnable{
BlockingQueue<Task> queue;
...
public void run(){
try{
queue.take();
}catch(InterruptedExcetion e){
Thread.currentThread().innterrupt();// 恢复被中断的状态
}
}
}