Java正确处理InterruptedException的方法(java并发编程第7章)

要想讨论正确处理InterrupedtException的方法,就要知道InterruptedException是什么。

根据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住做一些清理工作之后重抛该异常. 这样的处理使得你的方法也成为一个可中断的阻塞方法:

Java代码   收藏代码
  1. // 直接向上层抛出InterruptedException, dosomething方法也是一个可中断的阻塞方法  
  2. private void dosomething() throws InterruptedException {  
  3.     Thread.sleep(1000);  
  4. }  
  

2. 有时不能向上抛出InterruptedException异常(例如父类的相应方法没有声明该异常), 此时catch之后, 必须设置当前线程的中断标记为true, 以表明当前线程发生了中断, 以便调用栈上层进行处理:

Java代码   收藏代码
  1. public class InterruptedExceptionHandler implements Runnable {  
  2.     private Object lock = new Object();  
  3.   
  4.     @Override  
  5.     public void run() {  
  6.         while (!Thread.currentThread().isInterrupted()) {  
  7.             dosomething();  
  8.         }  
  9.     }  
  10.   
  11.     private void dosomething() {  
  12.         try {  
  13.             // Object.wait是一个可中断的阻塞方法, 如果在其阻塞期间检查到当前线程的中断标记为true, 会重置中断标记后从阻塞状态返回, 并抛出InterruptedException异常  
  14.             synchronized (lock) {  
  15.                 lock.wait();  
  16.             }  
  17.         } catch (InterruptedException e) {  
  18.             System.out.println("InterruptedException happened");  
  19.             // catch住InterruptedException后设置当前线程的中断标记为true, 以供调用栈上层进行相应的处理  
  20.             // 在此例中, dosomething方法的调用栈上层是run方法.  
  21.             Thread.currentThread().interrupt();  
  22.         }  
  23.     }  
  24.       
  25.     public static void main(String[] args) throws InterruptedException {  
  26.         Thread t = new Thread(new InterruptedExceptionHandler());  
  27.         t.start();  
  28.         // 启动线程1s后设置其中断标记为true  
  29.         Thread.sleep(1000);  
  30.         t.interrupt();  
  31.     }  
  32. }   

主线程启动InterruptedExceptionHandler线程1s后, 设置InterruptedExceptionHandler线程的中断标记为true. 此时InterruptedExceptionHandler线程应该阻塞在wait方法上, 由于wait方法是可中断的阻塞方法, 所以其检查到中断标记为true时, 将重置当前线程的中断标记后抛出InterruptedException, dosomething方法catch住InterruptedException异常后, 再次将当前线程的中断标记设置为true, run方法检查到中断标记为true, 循环不再继续. 假如dosomething方法catch住InterruptedException异常后没有设置中断标记, 其调用栈上层的run方法就无法得知线程曾经发生过中断, 循环也就无法终止.

 

3. 还有一种情形比较特殊: 我们希望发生了InterruptedException异常后仍然继续循环执行某阻塞方法, 此时应该将中断状态保存下来, 当循环完成后再根据保存下来的中断状态执行相应的操作:

Java代码   收藏代码
  1. public class InterruptedExceptionContinueHandler implements Runnable {  
  2.     private BlockingQueue<Integer> queue;  
  3.   
  4.     public InterruptedExceptionContinueHandler(BlockingQueue<Integer> queue) {  
  5.         this.queue = queue;  
  6.     }  
  7.   
  8.     @Override  
  9.     public void run() {  
  10.         while (!Thread.currentThread().isInterrupted()) {  
  11.             dosomething();  
  12.         }  
  13.         System.out.println(queue.size());  
  14.     }  
  15.   
  16.     private void dosomething() {  
  17.         // cancelled变量用于表明线程是否发生过中断  
  18.         boolean cancelled = false;  
  19.         for (int i = 0; i < 10000; i++) {  
  20.             try {  
  21.                 queue.put(i);  
  22.             } catch (InterruptedException e) {  
  23.                 // 就算发生了InterruptedException, 循环也希望继续运行下去, 此时将cancelled设置为true, 以表明遍历过程中发生了中断  
  24.                 System.out.println("InterruptedException happened when i = " + i);  
  25.                 cancelled = true;  
  26.             }  
  27.         }  
  28.         // 如果当前线程曾经发生过中断, 就将其中断标记设置为true, 以通知dosomething方法的上层调用栈  
  29.         if (cancelled) {  
  30.             Thread.currentThread().interrupt();  
  31.         }  
  32.     }  
  33.       
  34.     public static void main(String[] args) throws InterruptedException {  
  35.         Thread t = new Thread(new InterruptedExceptionContinueHandler(new LinkedBlockingQueue<Integer>()));  
  36.         t.start();  
  37.           
  38.         // 启动线程2ms后设置其中断标记为true  
  39.         Thread.sleep(2);  
  40.         t.interrupt();  
  41.     }  
  42. }  

在我的机器中, 输出结果如下:

InterruptedException happened when i = 936

size = 9999

队列的size是9999而不是10000, 是因为i = 936时发生了InterruptedException异常, 该次put没有成功.

为什么不在发生InterruptedException时就设置当前线程的中断标记, 而非要绕一圈? 假设将dosomething方法改为:

Java代码   收藏代码
  1. private void dosomething() {  
  2.     for (int i = 0; i < 10000; i++) {  
  3.         try {  
  4.             queue.put(i);  
  5.         } catch (InterruptedException e) {  
  6.             System.out.println("InterruptedException happened when i = " + i);  
  7.             Thread.currentThread().interrupt();  
  8.         }  
  9.     }  
  10. }  

运行后发现结果类似为:

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方法中, 且后续代码不检查中断状态时:

Java代码   收藏代码
  1. public static void main(String[] args) {  
  2.     // main方法已经是调用栈的最上层, 此时可以catchInterruptedException后不做任何处理  
  3.     try {  
  4.         Thread.sleep(2);  
  5.     } catch (InterruptedException e) {  
  6.         e.printStackTrace();  
  7.     }  
  8. }  


  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值