1 /**
測試當(dāng)前線程是否已中斷。 2 * Tests whether the current thread has been interrupted.
此方法清除線程的中斷狀態(tài)。
The <i>interrupted status</i> of the thread is cleared by this method.
換言之,如果連續(xù)兩次調(diào)用此方法,則第二次調(diào)用將返回false(除非在第一次調(diào)用清除其中斷狀態(tài)后,第二次調(diào)用檢查它之前,當(dāng)前線程再次中斷)。
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 * @如果當(dāng)前線程已中斷,則返回<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 /**
測試此線程是否已中斷。線程的中斷狀態(tài)不受此方法的影響。 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 /** 測試某個線程是否被中斷。中斷狀態(tài)是否基于傳遞的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 如果線程被設(shè)置中斷狀態(tài),且移除中斷狀態(tài),設(shè)置中斷狀態(tài)為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 }
總結(jié): Thread類中的static boolean interrupted()修改中斷狀態(tài)為false ,boolean isInterrupted()只判斷中斷狀態(tài),但不修改
public void interrupt() 將調(diào)用者線程的中斷狀態(tài)設(shè)置為true;
public boolean isInterrupted() 返回調(diào)用者線程的中斷狀態(tài);
public static boolean interrupted() 只能通過Thread.interrupted()調(diào)用。
它會做兩步操作:
1. 返回currentThread()的中斷狀態(tài);
2. 將當(dāng)前線程的中斷狀態(tài)修改為false;
浙公網(wǎng)安備 33010602011771號