深入解析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();
}