Thread类中提供了两个检测线程是否中断的方法,一个是静态方法调用,一个是成员方法调用。并且静态方法调用会清除当前线程的是否中断的状态。
参考资料:
jdk源码
/** * Tests whether the current thread has been interrupted. The * interrupted status 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). * *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
true
if the current thread has been interrupted; *false
otherwise. * @see #isInterrupted() * @revised 6.0 */ public static boolean interrupted() { return currentThread().isInterrupted(true); } /** * Tests whether this thread has been interrupted. The interrupted * status of the thread is unaffected by this method. * *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
true
if this thread has been interrupted; *false
otherwise. * @see #interrupted() * @revised 6.0 */ public boolean isInterrupted() { return isInterrupted(false); } /** * 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);