Thread实现原理

DHB大约 1 分钟JavaJvm源码

Thread实现原理

什么是线程

线程在Linux系统中也称做轻量级进程(LWP),是调度资源的最小单元,不同的线程间共享进程[1]中的数据,

[1] 进程是分配资源的最小单元。

Java线程

java线程中的native方法具体实现在 jvm.cpp 中,在 jdk/src/java.base/share/native/libjava/Thread.c 中是这样定义的。

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},
};

start实现

类/函数作用位置
MutexLocker::MutexLockermutex锁/jdk-jdk-9-181/hotspot/src/share/vm/runtime/mutexLocker.hpp:163
JNIHandles/jdk-jdk-9-181/hotspot/src/share/vm/runtime/jniHandles.hpp:269
os::start_thread开启线程jdk-jdk-9-181/hotspot/src/share/vm/runtime/os.cpp:774

断点位置

JVM_StartThread(JNIEnv*, jobject) at /home/dev/jdk/jdk-jdk-9-181/hotspot/src/share/vm/prims/jvm.cpp:2843
os::create_thread(Thread*, os::ThreadType, unsigned long) at /home/dev/jdk/jdk-jdk-9-181/hotspot/src/os/linux/vm/os_linux.cpp:698
JVM_StartThread(JNIEnv*, jobject) at /home/dev/jdk/jdk-jdk-9-181/hotspot/src/share/vm/prims/jvm.cpp:2777

生命周期

生命周期
生命周期
上次编辑于:
贡献者: dhb