Thread.interrupt()源码跟踪

Posted linzhanfly

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Thread.interrupt()源码跟踪相关的知识,希望对你有一定的参考价值。

1 JDK源码跟踪

// java.lang.Thread
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();


private native void interrupt0();

2 OpenJDK源码跟踪

  • jdk源码(JNI注册)
// jdk/src/share/native/java/lang/Thread.c:43
static JNINativeMethod methods[] = 
    "start0",           "()V",        (void *)&JVM_StartThread,
    "stop0",            "(" OBJ ")V", (void *)&JVM_StopThread,
    "isAlive",          "()Z",        (void *)&JVM_IsThreadAlive,
    "suspend0",         "()V",        (void *)&JVM_SuspendThread,
    "resume0",          "()V",        (void *)&JVM_ResumeThread,
    "setPriority0",     "(I)V",       (void *)&JVM_SetThreadPriority,
    "yield",            "()V",        (void *)&JVM_Yield,
    "sleep",            "(J)V",       (void *)&JVM_Sleep,
    "currentThread",    "()" THD,     (void *)&JVM_CurrentThread,
    "countStackFrames", "()I",        (void *)&JVM_CountStackFrames,
    "interrupt0",       "()V",        (void *)&JVM_Interrupt,
    "isInterrupted",    "(Z)Z",       (void *)&JVM_IsInterrupted,
    "holdsLock",        "(" OBJ ")Z", (void *)&JVM_HoldsLock,
    "getThreads",        "()[" THD,   (void *)&JVM_GetAllThreads,
    "dumpThreads",      "([" THD ")[[" STE, (void *)&JVM_DumpThreads,
    "setNativeName",    "(" STR ")V", (void *)&JVM_SetNativeThreadName,
;

// jdk/src/share/javavm/export/jvm.h:254
JNIEXPORT void JNICALL
JVM_Interrupt(JNIEnv *env, jobject thread);
  • java虚拟机(HotSpot实现):
// hotspot/src/share/prims/jvm.cpp:3289
JVM_ENTRY(void, JVM_Interrupt(JNIEnv* env, jobject jthread))
  JVMWrapper("JVM_Interrupt");

  // 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) 
    Thread::interrupt(thr);
  
JVM_END

// hotspot/src/share/vm/runtime/thraed.cpp:634
ParkEvent * _ParkEvent ;   // for synchronized()
ParkEvent * _SleepEvent ; // for Thread.sleep
ParkEvent * _MutexEvent ; // for native internal Mutex/Monitor,互斥锁
ParkEvent * _MuxEvent ;   // for low-level muxAcquire-muxRelease,共享锁

// hotspot/src/share/vm/runtime/thraed.cpp:804
void Thread::interrupt(Thread* thread) 
  trace("interrupt", thread);
  debug_only(check_for_dangling_thread_pointer(thread);)
  os::interrupt(thread);


// hotspot/src/hotspot/os/linux/vm/os_linux.cpp:4192
void os::interrupt(Thread* thread) 
  assert(Thread::current() == thread || Threads_lock->owned_by_self(),
    "possibility of dangling Thread pointer");

  OSThread* osthread = thread->osthread();

  if (!osthread->interrupted()) 
    osthread->set_interrupted(true);
    // More than one thread can get here with the same value of osthread,
    // resulting in multiple notifications.  We do, however, want the store
    // to interrupted() to be visible to other threads before we execute unpark().
    OrderAccess::fence();
    ParkEvent * const slp = thread->_SleepEvent ;
    // Thread.sleep方法继续运行
    if (slp != NULL) slp->unpark() ;
  

  // For JSR166. Unpark event if interrupt status already was set
  if (thread->is_Java_thread())
    ((JavaThread*)thread)->parker()->unpark();

  ParkEvent * ev = thread->_ParkEvent ;
  if (ev != NULL) ev->unpark() ;


// hotspot/src/share/vm/runtime/osThread.hpp:很短自己看
volatile jint _interrupted; // Thread.isInterrupted state    

volatile bool interrupted() const 
    return _interrupted != 0;


void set_interrupted(bool z) 
    _interrupted = z ? 1 : 0; 

以上是关于Thread.interrupt()源码跟踪的主要内容,如果未能解决你的问题,请参考以下文章

非常棒) Thread的中断机制(interrupt)

调用Thread.interrupt()方法到底会发生什么?

Thread的中断机制(interrupt)

Thread的中断机制(interrupt)

Thread的中断机制(interrupt),循环线程停止的方法

boost::thread interrupt() 仅在第一次执行中断点时引发异常?