本文共 1735 字,大约阅读时间需要 5 分钟。
调用t.interrupt()后,第一次返回true, 第2次及之后都返回false
其实都是空方法
public static boolean interrupted() { return currentThread().isInterrupted(true); // ClearInterrupted}
private native boolean isInterrupted(boolean ClearInterrupted);
JVM_QUICK_ENTRY(jboolean, JVM_IsInterrupted(JNIEnv* env, jobject jthread, jboolean clear_interrupted)) JVMWrapper("JVM_IsInterrupted"); // Ensure that the C++ Thread and OSThread structures aren't freed before we operate oop java_thread = JNIHandles::resolve_non_null(jthread); MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock); // We need to re-resolve the java_thread, since a GC might have happened during the // acquire of the lock JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)); if (thr == NULL) { return JNI_FALSE; } else { return (jboolean) Thread::is_interrupted(thr, clear_interrupted != 0); }JVM_END
volatile jint _interrupted; // Thread.isInterrupted statevoid set_interrupted(bool z) { _interrupted = z ? 1 : 0; }volatile bool interrupted() const { return _interrupted != 0; }
bool os::is_interrupted(Thread* thread, bool clear_interrupted) { assert(Thread::current() == thread || Threads_lock->owned_by_self(), "possibility of dangling Thread pointer"); OSThread* osthread = thread->osthread(); bool interrupted = osthread->interrupted(); // 返回interrupted状态 if (interrupted && clear_interrupted) { // 现在是中断状态 并且clear_interrupted=true 才会进来 osthread->set_interrupted(false); //设置false // consider thread->_SleepEvent->reset() ... optional optimization } return interrupted;}
转载地址:http://ruawb.baihongyu.com/