调用Thread.interrupt()方法到底会发生什么?

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.

时间: 2024-08-06 03:43:57

调用Thread.interrupt()方法到底会发生什么?的相关文章

Java Thread.interrupt方法

部分内容引用CSDNdr8737010 比如你在main线程中,开启了一个新的线程new Thread 首先,每个线程内部都有一个boolean型变量表示线程的中断状态,true代表线程处于中断状态,false表示未处于中断状态. 而interrupt()方法的作用只是用来改变线程的中断状态(把线程的中断状态改为true,即被中断). A线程调用wait,sleep,join方法,这时B线程调用了A的interrupt方法而抛出的InterruptedException是wait,sleep,j

Thread interrupt() 方法的使用

原文地址:http://blog.csdn.net/biangren/article/details/7522583 interrupt()只是改变中断状态而已:       interrupt()不会中断一个正在运行的线程.这一方法实际上完成的是,在线程受到阻塞时抛出一个中断信号,这样线程就得以退出阻塞的状态.更确切的说,如果线程被Object.wait, Thread.join和Thread.sleep三种方法之一阻塞,那么,它将接收到一个中断异常(InterruptedException)

停止Java线程,小心interrupt()方法

程序是很简易的.然而,在编程人员面前,多线程呈现出了一组新的难题,如果没有被恰当的解决,将导致意外的行为以及细微的.难以发现的错误. 在本篇文章中,我们针对这些难题之一:如何中断一个正在运行的线程. 背景     中断(Interrupt)一个线程意味着在该线程完成任务之前停止其正在进行的一切,有效地中止其当前的操作.线程是死亡.还是等待新的任务或是继续运行至下一步,就取决于这个程序.虽然初次看来它可能显得简单,但是,你必须进行一些预警以实现期望的结果.你最好还是牢记以下的几点告诫. 首先,忘掉

04 Thread的方法(源代码) 和 线程的状态

  1 .Thread中重要的属性 1 publicclass Thread implements Runnable { //继承自Runnable接口private char name[]; // 以char数组保存线程的名字 2 private int priority; // 线程优先级别 3 /* Whether or not the thread is a daemon thread. */ 4 private boolean daemon = false; //是否为守护线程 5 /

注意!你的Thread.Abort方法真的让线程停止了吗?

大家都知道在C#里面,我们可以使用 Thread.Start方法来启动一个线程,当我们想停止执行的线程时可以使用Thread.Abort方法来强制停止正在执行的线程,但是请注意,你确定调用了Thread.Abort方法后线程就立刻停止了吗? 答案是:不是! 下面我们来解释一下Abort方法是如何工作的.因为公用语言运行时管理了所有的托管的线程,同样它能在每个线程内抛出异常.Abort方法能在目标线程中抛出一个ThreadAbortException异常从而导致目标线程的终止.不过Abort方法被

Thread类的interrupt方法

一.线程中断 中断可以理解为一个线程的标志位,与线程中断有关的方法 interrupt方法 Thread类的实例方法:中断被调用线程,实际上只是改变了被调用线程 的内部中断状态, ==并不会中断线程== 必须注意API中的这一段描述 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(

C#多线程开发5:线程的Abort和Interrupt方法

使用线程的Abort方法可以终止线程:而使用线程的Interrupt方法只可以中断处于 WaitSleepJoin 状态的线程,当线程状态不再为 WaitSleepJoin时,线程将恢复执行.在线程上调用Abort方法会引发 ThreadAbortException异常,调用Interrupt方法则会引发ThreadInterruptedException异常. 下面的实例演示了Abort和Interrupt方法的使用. using System; using System.Threading;

HttpServlet的Service方法到底是怎么回事?

HttpServlet的Service方法到底是怎么回事?(视频下载) (全部书籍) 马克-to-win:谈到doGet,很多同学心中可能有疑问,为什么html的用户的GET的请求,会被Servlet的doGet方法处理呢?这就谈到了HttpServlet的Service方法.它的功能就是调用与HTTP请求的方法相对应的do功能.例如,如果HTTP请求方法为GET,则调用doGet() .这样作为Servlet编写者的你,只需覆盖doGet方法.这也是我们迄今为止的做法.有 意思的是,假如用户有

继承实现的原理、子类中调用父类的方法、封装

一.继承实现的原来 1.继承顺序 Python的类可以继承多个类.继承多个类的时候,其属性的寻找的方法有两种,分别是深度优先和广度优先. 如下的结构,新式类和经典类的属性查找顺序都一致.顺序为D--->A--->E--->B--->C. class E: def test(self): print('from E') class A(E): def test(self): print('from A') class B: def test(self): print('from B'