线程(二)OpenJDK 17 中线程启动的完整流程用C++ 源码详解之主-子线程通信机制
深入解析OpenJDK 17中Java线程的创建与主-子线程通信机制
引言
在Java中,线程的创建与启动通过Thread.start()
实现,但底层是JVM与操作系统协作完成的复杂过程。本文基于OpenJDK 17的C++源码,揭秘Java线程创建时主线程与子线程的通信机制,分析JVM如何通过同步原语(如Monitor
)实现线程状态的安全切换。
一、Java线程创建的核心流程
1. Java层到JVM层的调用链
当调用Thread.start()
时,JVM最终会执行JVM_StartThread
函数(位于jvm.cpp
),核心步骤如下:
cpp
复制
下载
JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread))// 1. 检查线程状态,防止重复启动if (java_lang_Thread::thread(jthread) != NULL) {// 抛出IllegalThreadStateException}// 2. 创建JavaThread对象,分配OS线程资源JavaThread* native_thread = new JavaThread(&thread_entry, stack_size);// 3. 初始化线程并启动Thread::start(native_thread); JVM_END
2. OS线程的创建(以Linux为例)
在os::create_thread
中,JVM通过pthread_create
创建操作系统线程:
cpp
复制
下载
bool os::create_thread(Thread* thread, ThreadType type, size_t stack_size) {// 分配OSThread对象OSThread* osthread = new OSThread();thread->set_osthread(osthread);// 初始化线程属性(栈大小、Guard Page等)pthread_attr_t attr;pthread_attr_init(&attr);pthread_attr_setstacksize(&attr, adjusted_stack_size);// 创建子线程,入口函数为thread_native_entryint ret = pthread_create(&tid, &attr, thread_native_entry, thread);// 等待子线程初始化完成Monitor* sync = osthread->startThread_lock();while (osthread->get_state() == ALLOCATED) {sync->wait_without_safepoint_check();} }
二、主线程与子线程的同步机制
1. 关键对象:Monitor与OSThread
-
OSThread
:每个线程的JVM内部表示,保存操作系统线程的元数据(如pthread_id
、状态等)。 -
Monitor
:JVM实现的同步锁,通过osthread->startThread_lock()
获取,用于协调主线程与子线程。
2. 同步过程详解
主线程的等待
cpp
复制
下载
// 主线程代码(在os::create_thread中) Monitor* sync = osthread->startThread_lock(); MutexLocker ml(sync); while (osthread->get_state() == ALLOCATED) {sync->wait_without_safepoint_check(); // 阻塞等待子线程信号 }
-
主线程在创建子线程后,通过
wait
进入阻塞状态,等待子线程初始化完成。
子线程的初始化与通知
子线程入口函数thread_native_entry
中:
cpp
复制
下载
static void* thread_native_entry(Thread* thread) {OSThread* osthread = thread->osthread();Monitor* sync = osthread->startThread_lock();// 初始化线程状态、栈、TLS等osthread->set_thread_id(os::current_thread_id());PosixSignals::hotspot_sigmask(thread);// 通知主线程{MutexLocker ml(sync);osthread->set_state(INITIALIZED);sync->notify_all(); // 唤醒主线程}// 继续执行线程任务thread->call_run(); }
-
子线程完成初始化后,通过
notify_all()
唤醒主线程。
三、核心源码解析
1. 状态机与竞态避免
-
线程状态流转:
ALLOCATED
→INITIALIZED
→RUNNABLE
主线程通过检查osthread->get_state()
确保子线程就绪后再继续。
2. 为何需要wait_without_safepoint_check
?
-
安全点(Safepoint):JVM在进行垃圾回收时,所有线程必须到达安全点。
-
wait_without_safepoint_check
:在等待期间禁用安全点检查,防止死锁(若等待时触发GC,主线程无法响应)。
3. 错误处理与资源释放
若线程创建失败(如内存不足):
cpp
复制
下载
if (native_thread->osthread() == NULL) {native_thread->smr_delete();THROW_MSG(OutOfMemoryError, "Cannot create native thread"); }
-
JVM清理已分配的资源(如
JavaThread
对象),并抛出OOM异常。
四、通信机制的意义
-
线程安全性:确保子线程完全初始化后,主线程才将其加入线程列表。
-
资源一致性:避免子线程未就绪时被误调度。
-
跨平台抽象:通过
Monitor
统一不同操作系统的线程同步细节。
五、总结与启示
-
同步的本质:主线程与子线程通过
Monitor
的等待-通知模型实现状态同步。 -
性能优化:
wait_without_safepoint_check
避免了安全点开销,提高线程创建效率。 -
资源管理:JVM严格处理线程创建失败场景,防止内存泄漏。
通过分析OpenJDK源码,我们不仅理解了Java线程的创建机制,更看到JVM如何通过精巧的同步设计,在复杂性与性能之间取得平衡。
##源码
JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread))JavaThread *native_thread = NULL;// We cannot hold the Threads_lock when we throw an exception,// due to rank ordering issues. Example: we might need to grab the// Heap_lock while we construct the exception.bool throw_illegal_thread_state = false;// We must release the Threads_lock before we can post a jvmti event// in Thread::start.{// Ensure that the C++ Thread and OSThread structures aren't freed before// we operate.MutexLocker mu(Threads_lock);// Since JDK 5 the java.lang.Thread threadStatus is used to prevent// re-starting an already started thread, so we should usually find// that the JavaThread is null. However for a JNI attached thread// there is a small window between the Thread object being created// (with its JavaThread set) and the update to its threadStatus, so we// have to check for thisif (java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)) != NULL) {throw_illegal_thread_state = true;} else {// We could also check the stillborn flag to see if this thread was already stopped, but// for historical reasons we let the thread detect that itself when it starts runningjlong size =java_lang_Thread::stackSize(JNIHandles::resolve_non_null(jthread));// Allocate the C++ Thread structure and create the native thread. The// stack size retrieved from java is 64-bit signed, but the constructor takes// size_t (an unsigned type), which may be 32 or 64-bit depending on the platform.// - Avoid truncating on 32-bit platforms if size is greater than UINT_MAX.// - Avoid passing negative values which would result in really large stacks.NOT_LP64(if (size > SIZE_MAX) size = SIZE_MAX;)size_t sz = size > 0 ? (size_t) size : 0;native_thread = new JavaThread(&thread_entry, sz);// At this point it may be possible that no osthread was created for the// JavaThread due to lack of memory. Check for this situation and throw// an exception if necessary. Eventually we may want to change this so// that we only grab the lock if the thread was created successfully -// then we can also do this check and throw the exception in the// JavaThread constructor.if (native_thread->osthread() != NULL) {// Note: the current thread is not being used within "prepare".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'.native_thread->smr_delete();if (JvmtiExport::should_post_resource_exhausted()) {JvmtiExport::post_resource_exhausted(JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_THREADS,os::native_thread_creation_failed_msg());}THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),os::native_thread_creation_failed_msg());}#if INCLUDE_JFRif (Jfr::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));}
#endifThread::start(native_thread);JVM_ENDbool os::create_thread(Thread* thread, ThreadType thr_type,size_t req_stack_size) {assert(thread->osthread() == NULL, "caller responsible");// Allocate the OSThread objectOSThread* osthread = new OSThread(NULL, NULL);if (osthread == NULL) {return false;}// set the correct thread stateosthread->set_thread_type(thr_type);// Initial state is ALLOCATED but not INITIALIZEDosthread->set_state(ALLOCATED);thread->set_osthread(osthread);// init thread attributespthread_attr_t attr;pthread_attr_init(&attr);pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);// Calculate stack size if it's not specified by caller.size_t stack_size = os::Posix::get_initial_stack_size(thr_type, req_stack_size);// In glibc versions prior to 2.7 the guard size mechanism// is not implemented properly. The posix standard requires adding// the size of the guard pages to the stack size, instead Linux// takes the space out of 'stacksize'. Thus we adapt the requested// stack_size by the size of the guard pages to mimick proper// behaviour. However, be careful not to end up with a size// of zero due to overflow. Don't add the guard page in that case.size_t guard_size = os::Linux::default_guard_size(thr_type);// Configure glibc guard page. Must happen before calling// get_static_tls_area_size(), which uses the guard_size.pthread_attr_setguardsize(&attr, guard_size);size_t stack_adjust_size = 0;if (AdjustStackSizeForTLS) {// Adjust the stack_size for on-stack TLS - see get_static_tls_area_size().stack_adjust_size += get_static_tls_area_size(&attr);} else {stack_adjust_size += guard_size;}stack_adjust_size = align_up(stack_adjust_size, os::vm_page_size());if (stack_size <= SIZE_MAX - stack_adjust_size) {stack_size += stack_adjust_size;}assert(is_aligned(stack_size, os::vm_page_size()), "stack_size not aligned");int status = pthread_attr_setstacksize(&attr, stack_size);if (status != 0) {// pthread_attr_setstacksize() function can fail// if the stack size exceeds a system-imposed limit.assert_status(status == EINVAL, status, "pthread_attr_setstacksize");log_warning(os, thread)("The %sthread stack size specified is invalid: " SIZE_FORMAT "k",(thr_type == compiler_thread) ? "compiler " : ((thr_type == java_thread) ? "" : "VM "),stack_size / K);thread->set_osthread(NULL);delete osthread;return false;}ThreadState state;{pthread_t tid;int ret = pthread_create(&tid, &attr, (void* (*)(void*)) thread_native_entry, thread);char buf[64];if (ret == 0) {log_info(os, thread)("Thread started (pthread id: " UINTX_FORMAT ", attributes: %s). ",(uintx) tid, os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));} else {log_warning(os, thread)("Failed to start thread - pthread_create failed (%s) for attributes: %s.",os::errno_name(ret), os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));// Log some OS information which might explain why creating the thread failed.log_info(os, thread)("Number of threads approx. running in the VM: %d", Threads::number_of_threads());LogStream st(Log(os, thread)::info());os::Posix::print_rlimit_info(&st);os::print_memory_info(&st);os::Linux::print_proc_sys_info(&st);os::Linux::print_container_info(&st);}pthread_attr_destroy(&attr);if (ret != 0) {// Need to clean up stuff we've allocated so farthread->set_osthread(NULL);delete osthread;return false;}// Store pthread info into the OSThreadosthread->set_pthread_id(tid);// Wait until child thread is either initialized or aborted{Monitor* sync_with_child = osthread->startThread_lock();MutexLocker ml(sync_with_child, Mutex::_no_safepoint_check_flag);while ((state = osthread->get_state()) == ALLOCATED) {sync_with_child->wait_without_safepoint_check();}}}// The thread is returned suspended (in state INITIALIZED),// and is started higher up in the call chainassert(state == INITIALIZED, "race condition");return true;
}// Thread start routine for all newly created threads
static void *thread_native_entry(Thread *thread) {thread->record_stack_base_and_size();#ifndef __GLIBC__// Try to randomize the cache line index of hot stack frames.// This helps when threads of the same stack traces evict each other's// cache lines. The threads can be either from the same JVM instance, or// from different JVM instances. The benefit is especially true for// processors with hyperthreading technology.// This code is not needed anymore in glibc because it has MULTI_PAGE_ALIASING// and we did not see any degradation in performance without `alloca()`.static int counter = 0;int pid = os::current_process_id();int random = ((pid ^ counter++) & 7) * 128;void *stackmem = alloca(random != 0 ? random : 1); // ensure we allocate > 0// Ensure the alloca result is used in a way that prevents the compiler from eliding it.*(char *)stackmem = 1;
#endifthread->initialize_thread_current();OSThread* osthread = thread->osthread();Monitor* sync = osthread->startThread_lock();osthread->set_thread_id(os::current_thread_id());if (UseNUMA) {int lgrp_id = os::numa_get_group_id();if (lgrp_id != -1) {thread->set_lgrp_id(lgrp_id);}}// initialize signal mask for this threadPosixSignals::hotspot_sigmask(thread);// initialize floating point control registeros::Linux::init_thread_fpu_state();// handshaking with parent thread{MutexLocker ml(sync, Mutex::_no_safepoint_check_flag);// notify parent threadosthread->set_state(INITIALIZED);sync->notify_all();// wait until os::start_thread()while (osthread->get_state() == INITIALIZED) {sync->wait_without_safepoint_check();}}log_info(os, thread)("Thread is alive (tid: " UINTX_FORMAT ", pthread id: " UINTX_FORMAT ").",os::current_thread_id(), (uintx) pthread_self());assert(osthread->pthread_id() != 0, "pthread_id was not set as expected");// call one more level start routinethread->call_run();// Note: at this point the thread object may already have deleted itself.// Prevent dereferencing it from here on out.thread = NULL;log_info(os, thread)("Thread finished (tid: " UINTX_FORMAT ", pthread id: " UINTX_FORMAT ").",os::current_thread_id(), (uintx) pthread_self());return 0;
}void os::pd_start_thread(Thread* thread) {OSThread * osthread = thread->osthread();assert(osthread->get_state() != INITIALIZED, "just checking");Monitor* sync_with_child = osthread->startThread_lock();MutexLocker ml(sync_with_child, Mutex::_no_safepoint_check_flag);sync_with_child->notify();
}