interrupted()
Posted xiaofan156
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了interrupted()相关的知识,希望对你有一定的参考价值。
1 /**
测试当前线程是否已中断。 2 * Tests whether the current thread has been interrupted.
此方法清除线程的中断状态。
The <i>interrupted status</i> of the thread is cleared by this method.
换言之,如果连续两次调用此方法,则第二次调用将返回false(除非在第一次调用清除其中断状态后,第二次调用检查它之前,当前线程再次中断)。
In other words, if this method were to be called twice in succession, the 5 * second call would return false (unless the current thread were 6 * interrupted again, after the first call had cleared its interrupted 7 * status and before the second call had examined it). 8 * 由于中断时线程不活动而忽略的线程中断将由返回false的方法反映。 9 * <p>A thread interruption ignored because a thread was not alive 10 * at the time of the interrupt will be reflected by this method 11 * returning false. 12 * @如果当前线程已中断,则返回<code>true</code>;否则返回<code>false</code>。 13 * @return <code>true</code> if the current thread has been interrupted; 14 * <code>false</code> otherwise. 15 * @see #isInterrupted() 16 * @revised 6.0 17 */ 18 public static boolean interrupted() { 19 return currentThread().isInterrupted(true); 20 }
1 /**
测试此线程是否已中断。线程的中断状态不受此方法的影响。 2 * Tests whether this thread has been interrupted. The <i>interrupted 3 * status</i> of the thread is unaffected by this method. 4 * 5 * <p>A thread interruption ignored because a thread was not alive 6 * at the time of the interrupt will be reflected by this method 7 * returning false. 8 * 9 * @return <code>true</code> if this thread has been interrupted; 10 * <code>false</code> otherwise. 11 * @see #interrupted() 12 * @revised 6.0 13 */ 14 public boolean isInterrupted() { 15 return isInterrupted(false); 16 }
1 /** 测试某个线程是否被中断。中断状态是否基于传递的ClearInterrupted值重置。 2 * Tests if some Thread has been interrupted. The interrupted state 3 * is reset or not based on the value of ClearInterrupted that is 4 * passed. 5 */ 6 private native boolean isInterrupted(boolean ClearInterrupted);
1 bool os::is_interrupted(Thread* thread, bool clear_interrupted) { 2 3 assert(Thread::current() == thread || Threads_lock->owned_by_self(), 4 5 "possibility of dangling Thread pointer"); 6 7 8 OSThread* osthread = thread->osthread(); 9 10 11 bool interrupted = osthread->interrupted(); 12 13 如果线程被设置中断状态,且移除中断状态,设置中断状态为false 14 if (interrupted && clear_interrupted) { 15 16 osthread->set_interrupted(false); 17 18 // consider thread->_SleepEvent->reset() ... optional optimization 19 20 } 21 22 23 return interrupted; 24 25 }
总结: Thread类中的static boolean interrupted()修改中断状态为false ,boolean isInterrupted()只判断中断状态,但不修改
public void interrupt() 将调用者线程的中断状态设置为true;
public boolean isInterrupted() 返回调用者线程的中断状态;
public static boolean interrupted() 只能通过Thread.interrupted()调用。
它会做两步操作:
1. 返回currentThread()的中断状态;
2. 将当前线程的中断状态修改为false;
以上是关于interrupted()的主要内容,如果未能解决你的问题,请参考以下文章
isInterrupted()方法和Thread.interrupted()方法判断中断状态的区别
解决SVN更新代码是出现previous operation has not finished; run cleanup if it was interrupted这个错误
svn "Previous operation has not finished; run 'cleanup' if it was interrupted”解决办法(示例代码