读书人

处理InterruptedException错误-JCIP7.

发布时间: 2012-09-01 09:33:03 作者: rapoo

处理InterruptedException异常--JCIP7.1读书笔记

[本文是我对Java Concurrency In Practice 7.1的归纳和总结. ?转载请注明作者和出处, ?如有谬误, 欢迎在评论中指正. ]

?

在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();@Overridepublic 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后设置其中断标记为trueThread.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;}@Overridepublic 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后设置其中断标记为trueThread.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();}}

读书人网 >编程

热点推荐