跳至主要內容
openjdk是怎么启动jvm的

java.c:1458

InitializeJVM函数初始化jvm虚拟机,

JavaMain以新的线程启动jvm

jvm

.h标识不同的系统实现方式不同

  • java.c:LoadJavaVM 加载libjvm.so动态链接库

  • java.c:ParseArguments 解析参数,找出mode(运行模式:main class、jar file)、what(主类)等等

  • java.h:JVMInit 主要执行ContinueInNewThread

  • java.c:ContinueInNewThread 创建java main方法的参数,并执行ContinueInNewThread0

  • java.h:ContinueInNewThread0 新建一个线程执行JavaMain函数,并阻塞(调用pthread_join

  • java.c:JavaMain

    • 调用InitializeJVM创建jvm
    • Main找出java主类(这里不一定是main方法,javaFX就没有main方法)
    • 构建主类的参数
    • 执行main方法

DHB大约 3 分钟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},
};

DHB小于 1 分钟JavaJvm源码