的实例java.lang.Thread
不是线程;它可用于表示 JVM 中的执行线程,但 JVM 完全能够在不使用Thread
该类的情况下创建线程。
这就是主线程发生的情况:JVM 创建它,然后创建一个实例java.lang.Thread
来表示它。
在 Hotspot JVM 中,在和中Threads
定义的类中有很多与线程相关的代码。JVM的启动调用静态函数,该函数已经在操作系统设置的线程中运行。在该函数中,我们发现:src/share/vm/runtime/thread.hpp
src/share/vm/runtime/thread.cpp
Threads::create_vm
(src/share/vm/runtime/thread.cpp)
3191 // Attach the main thread to this os thread
3192 JavaThread* main_thread = new JavaThread();
3193 main_thread->set_thread_state(_thread_in_vm);
3194 // must do this before set_active_handles and initialize_thread_local_storage
3195 // Note: on solaris initialize_thread_local_storage() will (indirectly)
3196 // change the stack size recorded here to one based on the java thread
3197 // stacksize. This adjusted size is what is used to figure the placement
3198 // of the guard pages.
3199 main_thread->record_stack_base_and_size();
3200 main_thread->initialize_thread_local_storage();
该类JavaThread
显然用于簿记;它将 OS 或 VM 线程与 Java Thread 对象相关联。Java 对象显然还不存在。然后代码继续初始化各种其他东西,然后在同一个函数中我们发现:
3335 // Initialize java_lang.System (needed before creating the thread)
3336 if (InitializeJavaLangSystem) {
3337 initialize_class(vmSymbols::java_lang_System(), CHECK_0);
3338 initialize_class(vmSymbols::java_lang_ThreadGroup(), CHECK_0);
3339 Handle thread_group = create_initial_thread_group(CHECK_0);
3340 Universe::set_main_thread_group(thread_group());
3341 initialize_class(vmSymbols::java_lang_Thread(), CHECK_0);
3342 oop thread_object = create_initial_thread(thread_group, main_thread, CHECK_0);
3343 main_thread->set_threadObj(thread_object);
3344 // Set thread status to running since main thread has
3345 // been started and running.
3346 java_lang_Thread::set_thread_status(thread_object,
3347 java_lang_Thread::RUNNABLE);
换句话说,我们初始化System
、ThreadGroup
和Thread
类,然后创建一个被Thread
引用的实例thread_object
(第 3342 行),并Thread
为 main 设置实例JavaThread
。
如果你想知道它做了什么create_initial_thread
,显然它分配 Thread 实例,在 Thread 实例的私有字段中存储指向JavaThread
(C++) 对象的指针eetop
,将线程优先级字段设置为正常,调用Thread(ThreadGroup group,String name)
构造函数,并返回实例:
967 // Creates the initial Thread
968 static oop create_initial_thread(Handle thread_group, JavaThread* thread, TRAPS) {
969 klassOop k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK_ NULL);
970 instanceKlassHandle klass (THREAD, k);
971 instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_NULL);
972
973 java_lang_Thread::set_thread(thread_oop(), thread);
974 java_lang_Thread::set_priority(thread_oop(), NormPriority);
975 thread->set_threadObj(thread_oop());
976
977 Handle string = java_lang_String::create_from_str("main", CHECK_NULL);
978
979 JavaValue result(T_VOID);
980 JavaCalls::call_special(&result, thread_oop,
981 klass,
982 vmSymbols::object_initializer_name(),
983 vmSymbols::threadgroup_string_void_signature(),
984 thread_group,
985 string,
986 CHECK_NULL);
987 return thread_oop();
988 }
现在,这就是 Hotspot VM 所做的。其他实现,如 IBM J9、Oracle JRockit 或 Azul Zing 可能会做类似的事情。