根据Java Doc的定义
Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity. Occasionally a method may wish to test whether the current thread has been interrupted, and if so, to immediately throw this exception.
意思是说当一个线程处于等待,睡眠,或者占用,也就是说阻塞状态,而这时线程被中断就会抛出这类错误,但是线程并没有被中断,任何时候,出现 该异常都需要手动在代码中再次中断该异常。Java6之后结束某个线程A的方法是A.interrupt()。如果这个线程正处于非阻塞状态,比如说线程正在执行某些代码的时候,不过被interrupt,那么该线程的interrupt变量会被置为true,告诉别人说这个线程被中断了(只是一个标志位,这个变量本身并不影响线程的中断与否),而且线程会被中断,这时不会有interruptedException。但如果这时线程被阻塞了,比如说正在睡眠,那么就会抛出这个错误。请注意,这个时候变量interrupt没有被置为true,而且也没有人来中断这个线程。这类阻塞操作中中断线程,需要做如下操作。
在java的中断机制中, InterruptedException异常占据重要的位置. 处理InterruptedException异常的方式有:
1. 不catch直接向上层抛出, 或者catch住做一些清理工作之后重抛该异常. 这样的处理使得你的方法也成为一个可中断的阻塞方法:
- // 直接向上层抛出InterruptedException, dosomething方法也是一个可中断的阻塞方法
- private void dosomething() throws InterruptedException {
- Thread.sleep(1000);
- }
2. 有时不能向上抛出InterruptedException异常(例如父类的相应方法没有声明该异常), 此时catch之后, 必须设置当前线程的中断标记为true, 以表明当前线程发生了中断, 以便调用栈上层进行处理:
- public class InterruptedExceptionHandler implements Runnable {
- private Object lock = new Object();
- @Override
- public void run() {
- while (!Thread.currentThread().isInterrupted()) {
- dosomething();
- }
- }
- private void dosomething() {
- try {
- // Object.wait是一个可中断的阻塞方法, 如果在其阻塞期间检查到当前线程的中断标记为true, 会重置中断标记后从阻塞状态返回, 并抛出InterruptedException异常
- synchronized (lock) {
- lock.wait();
- }
- } catch (InterruptedException e) {
- System.out.println("InterruptedException happened");
- // catch住InterruptedException后设置当前线程的中断标记为true, 以供调用栈上层进行相应的处理
- // 在此例中, dosomething方法的调用栈上层是run方法.
- Thread.currentThread().interrupt();
- }
- }
- public static void main(String[] args) throws InterruptedException {
- Thread t = new Thread(new InterruptedExceptionHandler());
- t.start();
- // 启动线程1s后设置其中断标记为true
- Thread.sleep(1000);
- t.interrupt();
- }
- }
主线程启动InterruptedExceptionHandler线程1s后, 设置InterruptedExceptionHandler线程的中断标记为true. 此时InterruptedExceptionHandler线程应该阻塞在wait方法上, 由于wait方法是可中断的阻塞方法, 所以其检查到中断标记为true时, 将重置当前线程的中断标记后抛出InterruptedException, dosomething方法catch住InterruptedException异常后, 再次将当前线程的中断标记设置为true, run方法检查到中断标记为true, 循环不再继续. 假如dosomething方法catch住InterruptedException异常后没有设置中断标记, 其调用栈上层的run方法就无法得知线程曾经发生过中断, 循环也就无法终止.
3. 还有一种情形比较特殊: 我们希望发生了InterruptedException异常后仍然继续循环执行某阻塞方法, 此时应该将中断状态保存下来, 当循环完成后再根据保存下来的中断状态执行相应的操作:
- public class InterruptedExceptionContinueHandler implements Runnable {
- private BlockingQueue<Integer> queue;
- public InterruptedExceptionContinueHandler(BlockingQueue<Integer> queue) {
- this.queue = queue;
- }
- @Override
- public void run() {
- while (!Thread.currentThread().isInterrupted()) {
- dosomething();
- }
- System.out.println(queue.size());
- }
- private void dosomething() {
- // cancelled变量用于表明线程是否发生过中断
- boolean cancelled = false;
- for (int i = 0; i < 10000; i++) {
- try {
- queue.put(i);
- } catch (InterruptedException e) {
- // 就算发生了InterruptedException, 循环也希望继续运行下去, 此时将cancelled设置为true, 以表明遍历过程中发生了中断
- System.out.println("InterruptedException happened when i = " + i);
- cancelled = true;
- }
- }
- // 如果当前线程曾经发生过中断, 就将其中断标记设置为true, 以通知dosomething方法的上层调用栈
- if (cancelled) {
- Thread.currentThread().interrupt();
- }
- }
- public static void main(String[] args) throws InterruptedException {
- Thread t = new Thread(new InterruptedExceptionContinueHandler(new LinkedBlockingQueue<Integer>()));
- t.start();
- // 启动线程2ms后设置其中断标记为true
- Thread.sleep(2);
- t.interrupt();
- }
- }
在我的机器中, 输出结果如下:
InterruptedException happened when i = 936
size = 9999
队列的size是9999而不是10000, 是因为i = 936时发生了InterruptedException异常, 该次put没有成功.
为什么不在发生InterruptedException时就设置当前线程的中断标记, 而非要绕一圈? 假设将dosomething方法改为:
- private void dosomething() {
- for (int i = 0; i < 10000; i++) {
- try {
- queue.put(i);
- } catch (InterruptedException e) {
- System.out.println("InterruptedException happened when i = " + i);
- Thread.currentThread().interrupt();
- }
- }
- }
运行后发现结果类似为:
InterruptedException happened when i = 936
InterruptedException happened when i = 937
...
InterruptedException happened when i = 9998
InterruptedException happened when i = 9999
size = 936
catch住InterruptedException后立即将当前线程的中断标记设置为true, 就会导致put方法又抛出InterruptedException异常, 如此往复直到循环结束.
4. 最不可取的是catch了InterruptedException异常但是不做任何处理, 这样一来调用栈上层就无法得知当前线程是否发生过中断. 只有一种情况下可以这样处理: 当InterruptedException发生在调用栈的最上层, 如run方法, 或者main方法中, 且后续代码不检查中断状态时:
- public static void main(String[] args) {
- // main方法已经是调用栈的最上层, 此时可以catchInterruptedException后不做任何处理
- try {
- Thread.sleep(2);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }