对于Java的InterceptException以及interrupt()等方法总会有疑惑,或者记不清,其实最好的解释就是方法的doc注释
咱们看看源码上,对于interrupt()方法的注释:
/** * Interrupts this thread. * * <p> Unless the current thread is interrupting itself, which is * always permitted, the {@link #checkAccess() checkAccess} method * of this thread is invoked, which may cause a {@link * SecurityException} to be thrown. * 除非是当前线程的自我中断,当然这是允许的,否则会调用线程的checkAccess方法来判断调用方是否有权限做此次更改, * 所以该方法可能会抛出SecurityException * * <p> If this thread is blocked in an invocation of the {@link * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link * Object#wait(long, int) wait(long, int)} methods of the {@link Object} * class, or of the {@link #join()}, {@link #join(long)}, {@link * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)}, * methods of this class, then its interrupt status will be cleared and it * will receive an {@link InterruptedException}. * 如果此线程阻塞在Object的wait()、或者Thread的join()、sleep()方法上,那么该线程的中断状态将会被清空, * 并且会得到InterruptedException * * <p> If this thread is blocked in an I/O operation upon an {@link * java.nio.channels.InterruptibleChannel InterruptibleChannel} * then the channel will be closed, the thread's interrupt * status will be set, and the thread will receive a {@link * java.nio.channels.ClosedByInterruptException}. * 如果此线程阻塞在InterruptibleChannel的I/O操作上,那么这个InterruptibleChannel将会被关闭,同时 * 设置该线程的中断状态,并且会得到ClosedByInterruptException * * <p> If this thread is blocked in a {@link 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 {@link * java.nio.channels.Selector#wakeup wakeup} method were invoked. * 如果此线程阻塞在多路复用器上,设置该线程的中断状态,并且从多路复用器的select方法立马返回,也许会是一个非零值, * 方法调用接口类似于调用了Selector#wakeup方法 * * <p> If none of the previous conditions hold then this thread's interrupt * status will be set. </p> * 除了上面这些情况,那么就会设置下线程的中断状态 * * <p> Interrupting a thread that is not alive need not have any effect. * 如果一个线程的状态已经不是alive了,那么中断该线程不会有任何影响 * * @throws SecurityException * if the current thread cannot modify this thread * * @revised 6.0 * @spec JSR-51 */ public void interrupt() { if (this != Thread.currentThread()) checkAccess(); synchronized (blockerLock) { Interruptible b = blocker; if (b != null) { interrupt0(); // Just to set the interrupt flag b.interrupt(this); return; } } interrupt0(); }
所以根据上面的注释可以看到,如果线程被非自己调用了interrupt方法的话,主要是下面的情况:
1. 线程阻塞在Object#wait()、或者Thread#join()、Thread#sleep()方法上:清除中断状态,得到InterruptedException,当然得到的InterruptedException也会往上抛。所以Object#wait()、或者Thread#join()、Thread#sleep()都会抛出InterruptedException 2. 线程阻塞在InterruptibleChannel的I/O操作上:那么该通道就会被关闭,设置该线程的中断状态,同时抛出ClosedByInterruptException。举个例子,譬如你在调用FileChannel#transferTo时,当数据还没传输完成,对该线程调用了interrupt方法,那么该线程会抛出ClosedByInterruptException 3. 如果此线程阻塞在多路复用器Selector上:设置该线程的中断状态,并且从多路复用器的select方法立马返回,也许会是一个非零值,方法调用接口类似于调用了Selector#wakeup方法。就是说如果当Selector#select()方法在调用,但是还没返回时,对线程调用了interrupt方法,那么其效果类似于,设置了中断状态,并调用了Selector#wakeup 4. 其他情况:设置中断状态
所以判断一个线程是否被中断,其实可以看看线程是否抛出了InterruptedException或者抛出ClosedByInterruptException或者其中断状态被设置了。
/** * Tests whether the current thread has been interrupted. The * <i>interrupted status</i> of the thread is cleared by this method. In * other words, if this method were to be called twice in succession, the * second call would return false (unless the current thread were * interrupted again, after the first call had cleared its interrupted * status and before the second call had examined it). * 判断当前线程是否被中断:该方法会清除中断状态 * * <p>A thread interruption ignored because a thread was not alive * at the time of the interrupt will be reflected by this method * returning false. * * @return <code>true</code> if the current thread has been interrupted; * <code>false</code> otherwise. * @see #isInterrupted() * @revised 6.0 */ public static boolean interrupted() { return currentThread().isInterrupted(true); }
可以看到interrupted()方法是Thread类的静态方法,方法注释写了,该方法在判断线程是否被中断的同时,会清除掉中断状态
/** * Tests whether this thread has been interrupted. The <i>interrupted * status</i> of the thread is unaffected by this method. * 判断当前线程是否被中断:中断状态不会被清除 * * <p>A thread interruption ignored because a thread was not alive * at the time of the interrupt will be reflected by this method * returning false. * * @return <code>true</code> if this thread has been interrupted; * <code>false</code> otherwise. * @see #interrupted() * @revised 6.0 */ public boolean isInterrupted() { return isInterrupted(false); }
可以看到isInterrupted()方法是Thread类的成员方法,方法注释写了,该方法在判断线程是否被中断的同时,不会对中断状态进行任何影响。
/** * 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);
代码如下(示例):
其实可以看到不管是interrupted方法还是isInterrupted()方法,调用的其实是Thread类里private的isInterrupted(boolean ClearInterrupted)方法,入参就是是否清楚中断状态,因为是native方法,就不再继续深入。
2. 可以看线程是否抛出了InterruptedException 3. 可以看线程是否抛出ClosedByInterruptException 4. 可以判断线程的中断状态是否被设置了
6. interrupted会清除中断状态,isInterrupted不会清除中断状态;所以对于同一次的interrupt,interrupted只有第一次调用会返回true,而isInterrupted会一直返回true 7. 其实两个方法底层实现是调用了同一个方法,只不过传入的参数ClearInterrupted(是否清除中断状态),一个传的是true,一个传的是false
多读源码注释,看不懂的英语,借助翻译软件即可~