当前位置: 首页 > article >正文

JUC编程之——线程的start方法及底层源码

1 一个小栗子

简单的写一个线程的小栗子:

public class ThreadTest extends Thread {
    @Override
    public void run() {
        System.out.println("In run");
    }
    public static void main(String []argv) {
        (new ThreadTest()).start();
    }
}

2 start()方法源码分析

2.1 start()源码

查看java start()方法的源码:

    public synchronized void start() {
        /**
         * This method is not invoked for the main method thread or "system"
         * group threads created/set up by the VM. Any new functionality added
         * to this method in the future may have to also be added to the VM.
         *
         * A zero status value corresponds to state "NEW".
         */
        if (threadStatus != 0)
            throw new IllegalThreadStateException();

        /* Notify the group that this thread is about to be started
         * so that it can be added to the group's list of threads
         * and the group's unstarted count can be decremented. */
        group.add(this);

        boolean started = false;
        try {
        	// 主要是这个函数
            start0();
            // started=true表示线程运行成功
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */
            }
        }
    }

2.2 start0()方法

	// start()方法调用start0()方法
	// native方法 调用底层C++方法
    private native void start0();

2.3 底层C++源码

更加底层的C++代码:

  • thread.c (代码位置:openJDK\src\share\native\java\lang)
  • jvm.cpp (代码位置:openIDK\hotspot\src\share\vm\prims)
  • thread.cpp (代码位置:openJDK\hotspot\src\share\vm\runtime)

代码——OpenJDK源码网址:http://openjdk.java.net/

2.3.1 thread.c

Thread.java对应thread.c源码

从.c的源码中可以看出start0()其实就是JVM_StartThread=>找到jvm.cpp

#include "jni.h"
#include "jvm.h"

#include "java_lang_Thread.h"

#define THD "Ljava/lang/Thread;"
#define OBJ "Ljava/lang/Object;"
#define STE "Ljava/lang/StackTraceElement;"
#define STR "Ljava/lang/String;"

#define ARRAY_LENGTH(a) (sizeof(a)/sizeof(a[0]))

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

#undef THD
#undef OBJ
#undef STE
#undef STR

JNIEXPORT void JNICALL
Java_java_lang_Thread_registerNatives(JNIEnv *env, jclass cls)
{
    (*env)->RegisterNatives(env, cls, methods, ARRAY_LENGTH(methods));
}

2.3.2 jvm.cpp

jvm.cpp部分源码:
主要知道第一行JVM_StartThread方法和最后一行Thread::start(native_thread)启动了线程;

// JVM_StartThread方法
JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread))
  JVMWrapper("JVM_StartThread");
  JavaThread *native_thread = NULL;

  bool throw_illegal_thread_state = false;
  {
    MutexLocker mu(Threads_lock);
    if (java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)) != NULL) {
      throw_illegal_thread_state = true;
    } else {
      jlong size =
             java_lang_Thread::stackSize(JNIHandles::resolve_non_null(jthread));
      size_t sz = size > 0 ? (size_t) size : 0;
      native_thread = new JavaThread(&thread_entry, sz);

      if (native_thread->osthread() != NULL) {
        native_thread->prepare(jthread);
      }
    }
  }

  if (throw_illegal_thread_state) {
    THROW(vmSymbols::java_lang_IllegalThreadStateException());
  }

  assert(native_thread != NULL, "Starting null thread?");

  if (native_thread->osthread() == NULL) {
    // No one should hold a reference to the 'native_thread'.
    delete native_thread;
    if (JvmtiExport::should_post_resource_exhausted()) {
      JvmtiExport::post_resource_exhausted(
        JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_THREADS,
        "unable to create new native thread");
    }
    THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
              "unable to create new native thread");
  }

#if INCLUDE_JFR
  if (JfrRecorder::is_recording() && EventThreadStart::is_enabled() &&
      EventThreadStart::is_stacktrace_enabled()) {
    JfrThreadLocal* tl = native_thread->jfr_thread_local();
    // skip Thread.start() and Thread.start0()
    tl->set_cached_stack_trace_id(JfrStackTraceRepository::record(thread, 2));
  }
#endif

  // 启动线程
  Thread::start(native_thread);

2.3.3 thread.cpp源码

最终是操作系统级别启动了start方法

void Thread::start(Thread* thread) {
  trace("start", thread);
  // Start is different from resume in that its safety is guaranteed by context or
  // being called from a Java method synchronized on the Thread object.
  if (!DisableStartThread) {
     // 初始化方法 
    if (thread->is_Java_thread()) {
      // Initialize the thread state to RUNNABLE before starting this thread.
      // Can not set it after the thread started because we do not know the
      // exact thread state at that time. It could be in MONITOR_WAIT or
      // in SLEEPING or some other state.
      java_lang_Thread::set_thread_status(((JavaThread*)thread)->threadObj(),
                                          java_lang_Thread::RUNNABLE);
    }
    // 最终是操作系统级别提高了start方法
    os::start_thread(thread);
  }
}

http://www.kler.cn/a/10574.html

相关文章:

  • Zotero 6.0 安装包及安装教程
  • 解决Anaconda出现CondaHTTPError: HTTP 000 CONNECTION FAILED for url
  • 九州未来再度入选2024边缘计算TOP100
  • 一种基于深度学习的反无人机无人值守系统及方法
  • 使用electron-egg把vue项目在linux Ubuntu环境下打包并安装运行
  • 动态规划 —— 子数组系列-最大子数组和
  • 用Kamailio修复FreeSWITCH的sdp
  • 信息系统项目管理师第四版知识摘编:第23章 组织通用管理​
  • 【MySQL】delete和truncate的用法和区别
  • 每个企业经营者都应该了解的几个网络安全趋势
  • Loki采集Mysql errorlog,你值得拥有的错误日志聚合系统
  • C#调试与测试 | Assert(断言)
  • 【Pytorch】神经网络搭建
  • 常用位运算和二进制做权限管理
  • 熟练了Flex布局之后,该学学Grid布局了
  • 前端项目-12-个人中心-二级路由配置-导航守卫-懒加载
  • Linux常见操作命令【三】
  • 算法模板(2):数据结构(3) 复杂数据结构1
  • Java14新特性
  • sql server存储过程
  • Java------Stream流式编程高级API【groupingBy、flatMap】(六)
  • 记2023第十四届蓝桥杯感受
  • 初识linux之线程控制
  • (二十三)槽函数的书写规则导致槽函数触发2次的问题
  • Microsoft Intune部署方案
  • 为pip永久性添加国内镜像安装源