首先,咱们说明下三个方法的功能java
interrupt()方法,可以将处于阻塞状态或等待状态(如wait()、 sleep() 、join()方法)的线程从阻塞状态跳出ide
今天在看到Thread类的isInterrupted方法能够获取线程的中断状态:this
因而写了个例子想验证一下:线程
public class Interrupt { public static void main(String[] args) throws Exception { Thread t = new Thread(new Worker()); t.start(); Thread.sleep(200); t.interrupt(); System.out.println("Main thread stopped."); } public static class Worker implements Runnable { public void run() { System.out.println("Worker started."); try { Thread.sleep(500); } catch (InterruptedException e) { System.out.println("Worker IsInterrupted: " + Thread.currentThread().isInterrupted()); } System.out.println("Worker stopped."); } } }
内容很简答:主线程main启动了一个子线程Worker,而后让worker睡500ms,而main睡200ms,以后main调用worker线程的interrupt方法去中断worker,worker被中断后打印中断的状态。下面是执行结果:设计
Worker started. Main thread stopped. Worker IsInterrupted: false Worker stopped.
Worker明明已经被中断,而isInterrupted()方法居然返回了false,为何呢?
code
在stackoverflow上搜索了一圈以后,发现有网友提到:能够查看抛出InterruptedException方法的JavaDoc(或源代码),因而我查看了Thread.sleep方法的文档,doc中是这样描述这个InterruptedException异常的:blog
InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown. 注意到后面这句“当抛出这个异常的时候,中断状态已被清除”。因此isInterrupted()方法应该返回false。但是有的时候,咱们须要isInterrupted这个方法返回true,怎么办呢?这里就要先说说interrupt, interrupted和isInterrupted的区别了:
interrupt方法是用于中断线程的,调用该方法的线程的状态将被置为"中断"状态。注意:线程中断仅仅是设置线程的中断状态位,不会中止线程。须要用户本身去监视线程的状态为并作处理。支持线程中断的方法(也就是线程中断后会抛出InterruptedException的方法,好比这里的sleep,以及Object.wait等方法)就是在监视线程的中断状态,一旦线程的中断状态被置为“中断状态”,就会抛出中断异常。文档
interrupt() merely sets the thread's interruption status. Code running in the interrupted thread can later poll the interrupted status to see if it has been requested to stop what it is doing
再来看看interrupted方法的实现:源码
public static boolean interrupted() { return currentThread().isInterrupted(true); }
和isInterrupted的实现:it
public boolean isInterrupted() { return isInterrupted(false); }
这两个方法一个是static的,一个不是,但实际上都是在调用同一个方法,只是interrupted方法传入的参数为true,而inInterrupted传入的参数为false。那么这个参数究竟是什么意思呢?来看下这个isInterrupted(boolean)方法的实现:
/** * Tests if some Thread has been interrupted. The interrupted state * is reset or not based on the value of ClearInterrupted that is * passed. */ private native boolean isInterrupted(boolean ClearInterrupted);
这是一个native方法,看不到源码没有关系,参数名字ClearInterrupted已经清楚的表达了该参数的做用----是否清除中断状态。方法的注释也清晰的表达了“中断状态将会根据传入的ClearInterrupted参数值肯定是否重置”。因此,静态方法interrupted将会清除中断状态(传入的参数ClearInterrupted为true),而实例方法isInterrupted则不会(传入的参数ClearInterrupted为false)。
回到刚刚的问题:很明显,若是要isInterrupted这个方法返回true,经过在调用isInterrupted方法以前再次调用interrupt()方法来恢复这个中断的状态便可:
public class Interrupt { public static void main(String[] args) throws Exception { Thread t = new Thread(new Worker()); t.start(); Thread.sleep(200); t.interrupt(); System.out.println("Main thread stopped."); } public static class Worker implements Runnable { public void run() { System.out.println("Worker started."); try { Thread.sleep(500); } catch (InterruptedException e) { Thread curr = Thread.currentThread(); //再次调用interrupt方法中断本身,将中断状态设置为“中断” curr.interrupt(); System.out.println("Worker IsInterrupted: " + curr.isInterrupted()); System.out.println("Worker IsInterrupted: " + curr.isInterrupted()); System.out.println("Static Call: " + Thread.interrupted());//clear status System.out.println("---------After Interrupt Status Cleared----------"); System.out.println("Static Call: " + Thread.interrupted()); System.out.println("Worker IsInterrupted: " + curr.isInterrupted()); System.out.println("Worker IsInterrupted: " + curr.isInterrupted()); } System.out.println("Worker stopped."); } } }
执行结果:
Worker started. Main thread stopped. Worker IsInterrupted: true Worker IsInterrupted: true Static Call: true ---------After Interrupt Status Cleared---------- Static Call: false Worker IsInterrupted: false Worker IsInterrupted: false Worker stopped.
从执行结果也能够看到,前两次调用isInterrupted方法都返回true,说明isInterrupted方法不会改变线程的中断状态,而接下来调用静态的interrupted()方法,第一次返回了true,表示线程被中断,第二次则返回了false,由于第一次调用的时候已经清除了中断状态。最后两次调用isInterrupted()方法就确定返回false了。
那么,在什么场景下,咱们须要在catch块里面中断线程(重置中断状态)呢?
答案是:若是不能抛出InterruptedException(就像这里的Thread.sleep语句放在了Runnable的run方法中,这个方法不容许抛出任何受检查的异常),但又想告诉上层调用者这里发生了中断的时候,就只能在catch里面重置中断状态了。
如下内容来自:Dealing with InterruptedException
If you catch InterruptedException but cannot rethrow it, you should preserve evidence that the interruption occurred so that code higher up on the call stack can learn of the interruption and respond to it if it wants to. This task is accomplished by calling interrupt() to "reinterrupt" the current thread, as shown in Listing 3. 若是捕捉到InterruptedException但不能从新抛出,则应保留中断发生的证据,以便调用堆栈上的较高层代码能够了解中断,并在须要时做出响应。这个任务是经过调用interrupt()来实现对当前线程的“从新中断”,如清单3所示。
Listing 3: Restoring the interrupted status after catching InterruptedException
public class TaskRunner implements Runnable { private BlockingQueue<Task> queue; public TaskRunner(BlockingQueue<Task> queue) { this.queue = queue; } public void run() { try { while (true) { Task task = queue.take(10, TimeUnit.SECONDS); task.execute(); } } catch (InterruptedException e) { // Restore the interrupted status Thread.currentThread().interrupt(); } } }
那么问题来了:为何要在抛出InterruptedException的时候清除掉中断状态呢? 这个问题没有找到官方的解释,估计只有Java设计者们才能回答了。但这里的解释彷佛比较合理:一个中断应该只被处理一次(你catch了这个InterruptedException,说明你能处理这个异常,你不但愿上层调用者看到这个中断)。