调用Thread.interrupt()方法到底会发生什么?
2017-05-21 18:29
302 查看
1. 当线程处于Blocked状态(sleep,wait,join),线程会退出阻塞状态,并抛出一个InterruptedException。park除外,它有响应但是不会抛出异常
2. 当线程处于Running状态,只是线程的interrupt标记被设置为true,线程本身的运行不会受到任何影响。
上面说得其实还不是特别准确,Java 1.8的Thread.interrupt()方法的注释如下,这个应该是最为权威准确的
Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.
If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.
If this thread is blocked in an I/O operation upon an InterruptibleChannel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a java.nio.channels.ClosedByInterruptException.
If this thread is blocked in a java.nio.channels.Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.
If none of the previous conditions hold then this thread's interrupt status will be set.
Interrupting a thread that is not alive need not have any effect.
2. 当线程处于Running状态,只是线程的interrupt标记被设置为true,线程本身的运行不会受到任何影响。
上面说得其实还不是特别准确,Java 1.8的Thread.interrupt()方法的注释如下,这个应该是最为权威准确的
Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.
If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.
If this thread is blocked in an I/O operation upon an InterruptibleChannel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a java.nio.channels.ClosedByInterruptException.
If this thread is blocked in a java.nio.channels.Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.
If none of the previous conditions hold then this thread's interrupt status will be set.
Interrupting a thread that is not alive need not have any effect.
相关文章推荐
- ResultSet.TYPE_SCROLL_SENSITIVE到底发生了什么?
- 不同种类的整型比较,到底发生了什么?
- linux 终端下敲ctrl-c时,到底发生了什么?
- 关于jQuery中的each方法(jQuery到底干了什么)
- 运算符重载时到底发生了什么?【DEBUG】
- 到底调用什么方法呢
- session.flush方法到底干了一些什么事情,举一个具体的例子说明
- java中 static变量和方法到底是存在内存什么区域呢?
- java中 static变量和方法到底是存在内存什么区域呢?
- 当你的iPhone收到推送信息后到底会发生什么呢?总共有三种可能性
- 受力的物体内部到底发生了什么情况—解释惯性力学三定律
- C++中include头文件到底会发生什么事?
- $(".class")后到底发生了什么 -- jquery选择器分析
- 请问基类中的虚方法到底有什么作用?
- C#参数传递时到底发生了什么二(ref)
- C#中引用类型做方法参数时,加ref或out,与不加ref或out到底有什么区别?
- 扩展方法到底做了什么
- C#参数传递时到底发生了什么一(一般传递方式)
- Python——maketrans和translate方法到底是什么玩意儿
- JavaScript有三种方法,可以确定一个值到底是什么类型。