interrupt()只是改变中断状态而已. interrupt()不会中断一个正在运行的线程。这一方法实际上完成的是,给受阻塞的线程抛出一个中断信号,这样受阻线程就得以退出阻塞的状态。更确切 的说,如果线程被Object.wait, Thread.join和Thread.sleep三种方法之一阻塞, 那么,它将接收到一个中断异常(InterruptedException),从而提早地终结被阻塞状态。
如果线程没有被阻塞,这时调用interrupt()将不起作用;否则,线程就将得到InterruptedException异常(该线程必须事先预备好处理此状况),接着逃离阻塞状态。
线程A在执行sleep,wait,join时,线程B调用线程A的interrupt方法,的确这一个时候A会有InterruptedException 异常抛出来.
但这其实是在sleep,wait,join这些方法内部会不断检查中断状态的值,而自己抛出的InterruptedException。
如果线程A正在执行一些指定的操作时如赋值,for,while,if,调用方法等,都不会去检查中断状态,所以线程A不会抛出 InterruptedException,而会一直执行着自己的操作.当线程A终于执行到wait(),sleep(),join()时,才马上会抛出 InterruptedException.若没有调用sleep(),wait(),join()这些方法,即没有在线程里自己检查中断状态自己抛出InterruptedException的 话,那InterruptedException是不会被抛出来的.
while(!Thread.isInterrupted()){
try {
Thread.sleep(1000);
}catch(InterruptedException ex)
{
Thread.interrupt()
}
}
分享到:
相关推荐
com interrupt void interrupt 4 using 3
Interrupt Example in MPLABX XC8 Compiler
MCS51 中断interrupt
ARM Generic Interrupt Spec, refenrence manual for arm operating sytem development
程序名称:interrupt.asm 程序说明:74HC595驱动数码管动态显示例,通过按键触发外部中断,P3.2进行计数,P3.3清零,实验箱上完成 算法说明:由两片74HC595芯片分别驱动段和位,数码管动态扫描显示:修改延时子...
interrupt,讲述飞思卡尔HCS12系列芯片的中断处理控制。
DOS 终端描述 bios interrupt , dos interrupt,interrupt services DOS,BIOS,EMS and MOUSE
The Generic Interrupt Controller (GIC) architecture defines: • the architectural requirements for handling all interrupt sources for any processor connected to a GIC • a common interrupt controller ...
微机接口课程实验interrupt.asm
ARM Generic Interrupt Controller Architecture Specification (GIC architecture version 3.0 and version 4.0)
Detection of Excessive Interrupt Disablement - a new feature which can detect a period of excessive interrupt disablement on a CPU, and create an error log record to report it. This allows you to know...
Ralf Brown's Interrupt List 中断大全
ARM Generic Interrupt Controller Architecture Specification 官方手册
解释void timer0() interrupt 1 using 2
电子-7interrupt.pdf,物联网/通信技术2.4G无线通信
Interrupt Services DOS, BIOS, EMS and Mouse 重温旧时代的DOS/BIOS资料,玩裸机编程也可以参考的。 非扫描pdf e文
switch_counter_interrupt_TivaWare.c
interrupt code
线程的thread.interrupt()方法是中断线程,将会设置该线程的中断状态位,即设置为true,中断的结果线程是死亡、还是等待新的任务或是继续运行至下一步,就取决于这个程序本身。线程会不时地检测这个中断标示位,以...