如何用Java创建主线程 [英] How main thread created by Java
问题描述
我是Java技术的新手。我知道只有两种方法可以在Java中创建 Thread
I am new to Java technology. I know that there is only two ways to create Thread
in Java
- 扩展线程class
- 实现Runnable接口
所以这只是创建<$ c $的两种方法C>螺纹。但是当我们用主JVM启动我们的程序时,启动了一个主要的 Thread
。我认为即使JVM必须遵循创建主 Thread的规则
创建主线程的方法JVM要么必须扩展Thread类,要么实现 Runnable
。
So this is only two ways of creating Thread
. But when we start our Program with main JVM started one main Thread
. I think even JVM has to follow the rule for creating main Thread
means for creating main thread JVM either has to extend Thread class or implement Runnable
.
public class MainThreadExample {
public static void main(String[] args) {
Thread t=Thread.currentThread();
System.out.println(t.getName());
}
}
我尝试了我的水平,但不知道如何JVM创建了这个主要对象。当我完全通过主类( sun.tool.jar
)时,我知道这是负责主线程的类。但是在Google搜索了这么多网页后没有能够得到它。所以,请帮助,如果可能的话,请参考我的示例或链接。
I tried my level best but not able to know how JVM created this main object. As I entirely went through main class (sun.tool.jar
) I know this is the class which is responsible for main thread.But after searching so many web page in Google not able to get it. So please help and if possible refer me the example or link also.
PS:我正在学习Java技术,我不应该打扰他们如何创建main以及它的全部内容设计的东西。但我认为这是一个合乎逻辑的问题
P.S: I am learning Java technology, I should not have bother this how they created main and all it's a designing thing. But I think its a logical question to ask
推荐答案
java.lang.Thread的实例
不是线程;它可以用来表示JVM中的执行线程,但是JVM完全能够在不使用 Thread
类的情况下创建线程。
An instance of java.lang.Thread
is not a thread; it can be used to represent a thread of execution in the JVM but the JVM is perfectly capable of creating threads without using the Thread
class at all.
这是主线程发生的事情:JVM创建它,并创建一个 java.lang.Thread
的实例以便稍后表示它。
This is what happens with the main thread: the JVM creates it, and an instance of java.lang.Thread
is created to represent it later.
在Hotspot JVM中, Threads 类中有很多与线程相关的代码http://hg.openjdk.java.net/jdk7/jdk7/hotspot/file/tip/src/share/vm/runtime/thread.hpp\">src/share/vm/runtime/ thread.hpp
和 的src /共享/ VM /运行/ thread.cpp
。 JVM的启动调用静态 Threads :: create_vm
函数,该函数已在操作系统设置的线程中运行。在该函数中,我们发现:
In the Hotspot JVM there's a lot of threading related code in the Threads
class defined in src/share/vm/runtime/thread.hpp
and src/share/vm/runtime/thread.cpp
. The startup of the JVM calls the static Threads::create_vm
function, which is already running in a thread set up by the operating system. Within that function we find:
(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对象显然还不存在。然后代码继续初始化各种其他内容,稍后仍然在相同的函数中我们发现:
The JavaThread
class is apparently used for bookkeeping; it associates an OS or VM thread with a Java Thread object. The Java object apparently doesn't exist yet. The code then goes on to initialize various other things, and later on still in the same function we find this:
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);
换句话说,我们初始化系统
, ThreadGroup
和 Thread
类,然后创建 Thread $ c的实例$ c>由
。 thread_object
引用(第3342行),并为主 Thread
实例> JavaThread
In other words, we it initializes the System
, ThreadGroup
, and Thread
classes, then creates an instance of Thread
referenced by thread_object
(line 3342), and sets the Thread
instance for the main JavaThread
.
如果你想知道 create_initial_thread
是做什么的,显然它会分配线程实例,在Thread实例的私有 eetop
字段中存储指向 JavaThread
(C ++)对象的指针集线程优先级字段为正常,调用线程(ThreadGroup组,字符串名称)
构造函数,并返回实例:
If you wonder what the create_initial_thread
does, apparently it allocates the Thread instance, stores a pointer to the JavaThread
(C++) object in the private eetop
field of the Thread instance, sets the thread priority field to normal, calls the Thread(ThreadGroup group,String name)
constructor, and returns the instance:
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可能会做类似的事情。
Now, this is what the Hotspot VM does. Other implementations, like IBM J9, Oracle JRockit, or Azul Zing probably do something similar though.
这篇关于如何用Java创建主线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!