interrupt & InterruptException
2010-06-02 01:03
169 查看
JDK1.6中的interrupt函数:
public void interrupt() 中断线程
如果当前线程没有中断它自己(这在任何情况下都是允许的),则该线程的 checkAccess 方法就会被调用,这可能抛出 SecurityException。
如果线程在调用 Object 类的 wait()、wait(long) 或 wait(long, int) 方法,或者该类的 join()、join(long)、join(long, int)、sleep(long) 或 sleep(long, int) 方法过程中受阻,则其中断状态将被清除,它还将收到一个 InterruptedException。
如果该线程在可中断的通道上的 I/O 操作中受阻,则该通道将被关闭,该线程的中断状态将被设置并且该线程将收到一个 ClosedByInterruptException。
如果该线程在一个 Selector 中受阻,则该线程的中断状态将被设置,它将立即从选择操作返回,并可能带有一个非零值,就好像调用了选择器的 wakeup 方法一样。
如果以前的条件都没有保存,则该线程的中断状态将被设置。
中断一个不处于活动状态的线程不需要任何作用。
抛出:SecurityException - 如果当前线程无法修改该线程
线程调用interrupt()本身并不抛出InterruptException异常, 而是在线程处于等待或睡眠状态时若被中断(取消)则会抛出InterruptException,即由sleep(),wait()抛出InterruptException,便于我们对线程的提前中断做出处理。(为什么说是提前中断呢,因为线程sleep或wait是在等待资源满足或某条件触发,在未满足前被强制中断)
更多InterruptException处理见:Java处理与实践:处理 InterruptedException
public void interrupt() 中断线程
如果当前线程没有中断它自己(这在任何情况下都是允许的),则该线程的 checkAccess 方法就会被调用,这可能抛出 SecurityException。
如果线程在调用 Object 类的 wait()、wait(long) 或 wait(long, int) 方法,或者该类的 join()、join(long)、join(long, int)、sleep(long) 或 sleep(long, int) 方法过程中受阻,则其中断状态将被清除,它还将收到一个 InterruptedException。
如果该线程在可中断的通道上的 I/O 操作中受阻,则该通道将被关闭,该线程的中断状态将被设置并且该线程将收到一个 ClosedByInterruptException。
如果该线程在一个 Selector 中受阻,则该线程的中断状态将被设置,它将立即从选择操作返回,并可能带有一个非零值,就好像调用了选择器的 wakeup 方法一样。
如果以前的条件都没有保存,则该线程的中断状态将被设置。
中断一个不处于活动状态的线程不需要任何作用。
抛出:SecurityException - 如果当前线程无法修改该线程
线程调用interrupt()本身并不抛出InterruptException异常, 而是在线程处于等待或睡眠状态时若被中断(取消)则会抛出InterruptException,即由sleep(),wait()抛出InterruptException,便于我们对线程的提前中断做出处理。(为什么说是提前中断呢,因为线程sleep或wait是在等待资源满足或某条件触发,在未满足前被强制中断)
更多InterruptException处理见:Java处理与实践:处理 InterruptedException
相关文章推荐
- vivado sdk生成elf文件出错:make: Interrupt/Exception caught (code = 0xc00000fd, addr = 0x4227d3)
- 中断(interrupt)、异常(exception)、陷入(trap)
- 中断(interrupt)、异常(exception)、陷入(trap)
- ULK --- Chap 4: Nested Execution of Exception and Interrupt Handlers
- interrupt and exception (一)
- win10环境下make: Interrupt/Exception caught (code = 0xc0000005, addr = 0x0040b0ac)
- x86 exception and interrupt handling
- 关于hadoop的java.nio.channels.ClosedByInterruptException
- Mingw32-make “Interrupt/Exception caught (code = 0xc00000fd, addr = 0x421963)
- [OS]Exception and Interrupt
- InterruptedException+interrupt停止线程的疑惑
- Understanding the linux kernel-ch4-Interrupt and exception
- ecos vector.S 分析II: exception/interrupt
- 中断(interrupt)、异常(exception)、陷入(trap)
- Linux interrupt & exception
- Difference between software interrupt, exception, trap
- Exception and Interrupts Notes of 80386 Programmer manual
- 《Understanding the Linux kernel》学习笔记 Chapter 4: Interrupts and ExceptionsM
- 关于hive的ClosedByInterruptException异常新动向3
- Difference between software interrupt, exception, trap