当一个方法抛出 InterruptedException
时,它是在告诉您,如果执行该方法的线程被中断,它将尝试停止它正在做的事情而提前返回,并通过抛出 InterruptedException
表明它提前返回。
一旦你catch了中断异常,默认你就会处理它,所以JVM会清除中断状态,以防止方法退出后上层调用代码再处理一次异常。如果你只是简单地catch住,并不打算处理,那么就需要恢复被清除的中断位,让上层知晓,所以需要调用interrupt()
切忌生吞InterruptedException
异常,正确的做法:
1、将异常传递给调用方。
public class TaskQueue { private static final int MAX_TASKS = 1000; private BlockingQueue<Task> queue = new LinkedBlockingQueue<Task>(MAX_TASKS); public void putTask(Task r) throws InterruptedException { queue.put(r); } public Task getTask() throws InterruptedException { return queue.take(); } }
2、在重新抛出 InterruptedException 之前执行特定于任务的清理工作。
public class PlayerMatcher { private PlayerSource players; public PlayerMatcher(PlayerSource players) { this.players = players; } public void matchPlayers() throws InterruptedException { try { Player playerOne, playerTwo; while (true) { playerOne = playerTwo = null; // Wait for two players to arrive and start a new game playerOne = players.waitForPlayer(); // could throw IE playerTwo = players.waitForPlayer(); // could throw IE startNewGame(playerOne, playerTwo); } } catch (InterruptedException e) { // If we got one player and were interrupted, put that player back if (playerOne != null) players.addFirst(playerOne); // Then propagate the exception throw e; } } }3、捕捉 InterruptedException 后恢复中断状态
例如当由 Runnable 定义的任务调用一个可中断的方法时,就是如此。在这种情况下,不能重新抛出 InterruptedException,但是您也不想什么都不做。当一个阻塞方法检测到中断并抛出 InterruptedException 时,它清除中断状态。如果捕捉到 InterruptedException 但是不能重新抛出它,那么应该保留中断发生的证据,以便调用栈中更高层的代码能知道中断,并对中断作出响应。该任务可以通过调用 interrupt() 以 “重新中断” 当前线程来完成。
相关推荐
│ 高并发编程第一阶段15讲、Thread中断Interrupt方法详细讲解.mp4 │ 高并发编程第一阶段16讲、采用优雅的方式结束线程生命周期.mp4 │ 高并发编程第一阶段17讲、Thread API综合实战,编写ThreadService实现暴力...
│ 高并发编程第一阶段15讲、Thread中断Interrupt方法详细讲解.mp4 │ 高并发编程第一阶段16讲、采用优雅的方式结束线程生命周期.mp4 │ 高并发编程第一阶段17讲、Thread API综合实战,编写ThreadService实现暴力...
难得的关于异常和中断的好书,老外写的.
第一章 Java 并发编程实践基础..............................................................1 1.1 进程与线程.................................................................................................
线程中断规则:对线程interrupt()方法的调用先行发生于被中断线程的代码检测到中断事件的发生; g.线程终结规则:线程中所有的操作都先行发生于线程的终止检测,我们可以通过Thread.join()方法结束、Thread.isAlive...
interrupt code
interrupt,讲述飞思卡尔HCS12系列芯片的中断处理控制。
windows系统蓝屏,查询历史蓝屏代码,分析蓝屏原因,提供解决方案
在学习Java过程中,自己收集了很多的Java的学习资料,分享给大家,有需要的欢迎下载,希望对大家有用,一起学习,一起进步。
MCS51 中断interrupt
IRQ interrupt function driver
本文介绍了在编写一个测试程序时,程序需要暂停一段时间,于是调用Thread.sleep()。但是编译器或IDE报错说没有处理检查到的InterruptedException。对于InterruptedException,一种常见的处理方式是“生吞” 它,然后...
Interrupt Example in MPLABX XC8 Compiler
ARM Generic Interrupt Spec, refenrence manual for arm operating sytem development
当调用线程的interrupt()方法时,会抛出InterruptException异常。阻塞中的那个方法抛出这个异常,通过代码捕获该异常,然后break跳出循环状态,从而让我们有机会结束这个线程的执行。通常很多人认为只要调用...
• Outstanding processing performance combined with fast interrupt handling • Enhanced system debug with extensive breakpoint and trace capabilities • Efficient processor core, system and memories ...
com interrupt void interrupt 4 using 3
电子-7interrupt.pdf,物联网/通信技术2.4G无线通信
微机接口课程实验interrupt.asm