简体   繁体   English

Java:如何生成新线程? 不是如何生成一个新的线程来使用

[英]Java: how to generate a new thread? Not how to generate a new thread to use

I don't know how Java can generate a new thread. 我不知道Java如何生成新线程。 I look into the Thread class in JDK and the init method may the key method. 我研究了JDK中的Thread类, init方法可能是关键方法。 It seems some work done by JVM? 看来JVM完成了一些工作? the new Thread seems to be in the same ThreadGroup of it's parent. 新线程似乎与其父线程位于同一线程组中。 But I'm not sure. 但是我不确定。 So I want ro know the exact flow of how Java generate a new Thread? 所以我想让RO知道Java生成新线程的确切流程吗? Form JVM or other Mechanism ? 形成JVM或其他机制? and how to get the resource from the OS? 以及如何从OS获取资源? Can you give me some document or site? 能给我一些文件或网站吗?

This might be JVM specific but how it works in Hotspot JVM: 这可能是特定于JVM的,但是它如何在Hotspot JVM中工作:

1) first when you create a Thread object, which is just a regular Java object the whole state required by a native thread is set: thread-local storage, buffers, stacks, program counter etc. 1)首先,当您创建一个Thread对象时,它只是一个普通的Java对象,设置了本机线程所需的整个状态:线程本地存储,缓冲区,堆栈,程序计数器等。

2) With this in place the JVM can create a native thread. 2)有了这个,JVM可以创建一个本机线程。 The JVM has a mapping of Thread objects and native threads so the native thread can be stopped when the Thread object gets stops. JVM具有Thread对象和本机线程的映射,因此可以在Thread对象停止时停止本机线程。

3) As you can see underneath it's just a native OS thread so the OS takes care of scheduling. 3)如您所见,它只是一个本机OS线程,因此OS负责调度。

4) When the run() method exits you can have uncaught exceptions though and therefore the JVM needs to check if it needs to be terminated or not. 4)但是,当run()方法退出时,您可能会遇到未捕获的异常,因此JVM需要检查它是否需要终止。 If no exceptions occurred all the resources from both the Thread object and the native thread are reclaimed. 如果没有异常发生,则将回收Thread对象和本机线程的所有资源。

Some more info: 更多信息:

Excellent JVM & Thread internals details here: http://blog.jamesdbloom.com/JVMInternals.html 优秀的JVM和线程内部细节在这里: http : //blog.jamesdbloom.com/JVMInternals.html

A thread is a thread of execution in a program. 线程是程序中的执行线程。 In the Hotspot JVM there is a direct mapping between a Java Thread and a native operating system Thread. 在Hotspot JVM中,Java线程和本机操作系统线程之间存在直接映射。 After preparing all of the state for a Java thread such as thread-local storage, allocation buffers, synchronization objects, stacks and the program counter, the native thread is created. 在为Java线程准备了所有状态(例如线程本地存储,分配缓冲区,同步对象,堆栈和程序计数器)之后,将创建本机线程。 The native thread is reclaimed once the Java thread terminates. Java线程终止后,将回收本机线程。 The operating system is therefore responsible for scheduling all threads and dispatching them to any available CPU. 因此,操作系统负责调度所有线程并将其分配给任何可用的CPU。 Once the native thread has initialized it invokes the run() method in the Java thread. 一旦本机线程初始化,它将调用Java线程中的run()方法。 When the run() method returns, uncaught exceptions are handled, then the native thread confirms if the JVM needs to be terminated as a result of the thread terminating (ie is it the last non-deamon thread). 当run()方法返回时,将处理未捕获的异常,然后本机线程确认是否由于线程终止(例如,它是最后一个非守护线程)而需要终止JVM。 When the thread terminates all resources for both the native and Java thread are released. 当线程终止时,将释放本机线程和Java线程的所有资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM