再回到我们的 start0 方法,此时我们就去查找 JVM_StartThread
方法是在他是在/hotspot/src/share/vm/prims/jvm.cpp
这个文件里面:
JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread)) JVMWrapper("JVM_StartThread"); 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); // 1. 判断 Java 线程是否启动,如果已经启动,抛出异常 if (java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)) != NULL) { throw_illegal_thread_state = true; } else { // 2. 如果没有创建,则会创建线程 jlong size = java_lang_Thread::stackSize(JNIHandles::resolve_non_null(jthread)); size_t sz = size > 0 ? (size_t) size : 0; // 虚拟机创建 JavaThread, 该类内部会创建操作系统线程,然后关联 Java 线程 native_thread = new JavaThread(&thread_entry, sz); 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'. 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"); } // 设置线程状态为 Runnable Thread::start(native_thread); JVM_END
JavaThread
类的构造方法我们一起来看看,他是通过 os::create_thread
函数来进行创建 Java 对应的内核线程
JavaThread::JavaThread(ThreadFunction entry_point, size_t stack_sz) : Thread() { if (TraceThreadEvents) { tty->print_cr("creating thread %p", this); } initialize(); _jni_attach_state = _not_attaching_via_jni; set_entry_point(entry_point); os::ThreadType thr_type = os::java_thread; thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread : os::java_thread; // 创建Java线程对应的内核线 os::create_thread(this, thr_type, stack_sz); _safepoint_visible = false; }
os:create_thread
其实主要就是一个用来支持跨平台创建线程的, 以 Linux 为例 (hotspot/src/os/linux/vm/os_linux.cpp):
bool os::create_thread(Thread* thread, ThreadType thr_type, size_t stack_size) { // ... // 创建 OSThread 内核线程对象 OSThread* osthread = new OSThread(NULL, NULL); // 绑定 thread->set_osthread(osthread); pthread_t tid; // pthread_create 为 linux api 用来创建线程。 int ret = pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread); // ... return true; }
我们可以通过 ubantu 的控制台来查询接口信息
man pthread_create 来进行查询文档
通过文档我们可以了解,当 pthread_create
函数执行创建完线程之后会调用第三个参数传递过去的回调函数
int ret = pthread_create(&tid, &attr, (void* ()(void)) java_start, thread);
在这里就是 java_start
函数
// Thread start routine for all newly created threads static void *java_start(Thread *thread) { // 主要是调用 Thread 的 run 方法 thread->run(); return 0; }
在 thread.cpp
中 JavaThread::run 方法最终调用了 thread_main_inner
方法:
// The first routine called by a new Java thread void JavaThread::run() { // We call another function to do the rest so we are sure that the stack addresses used // from there will be lower than the stack base just computed thread_main_inner(); // Note, thread is no longer valid at this point! }
在 thread_main_inner
方法内,在调用咱们之前创建 JavaThread
对象的时候传递进来的 entry_point
方法:
void JavaThread::thread_main_inner() { if (!this->has_pending_exception() && !java_lang_Thread::is_stillborn(this->threadObj())) { { ResourceMark rm(this); this->set_native_thread_name(this->get_thread_name()); } HandleMark hm(this); // 调用 entry_point 方法 this->entry_point()(this, this); } DTRACE_THREAD_PROBE(stop, this); this->exit(false); delete this; }
通过上面的代码我们可以看到先创建了一个 JavaThread 对象, 然后传入了 thread_entry
方法
// JVM_StartThread 创建操作系统线程,执行 thread_entry 函数 static void thread_entry(JavaThread* thread, TRAPS) { HandleMark hm(THREAD); Handle obj(THREAD, thread->threadObj()); JavaValue result(T_VOID); // Thrad.start() 调用 java.lang.Thread 类的 run 方法 JavaCalls::call_virtual(&result, obj, KlassHandle(THREAD, SystemDictionary::Thread_klass()), vmSymbols::run_method_name(), vmSymbols::void_method_signature(), THREAD); }
我们再来看看我们 Java 中 Thread 类的 run 方法
public void run() { if (target != null) { // Thread.run() 又调用 Runnable.run() target.run(); } }