This article says how to understand threads from a JVM's point of view, you can have a deeper understanding of the Java threading model, and some of the details of the GC will be more deeply understood. This paper is based on the OpenJDK7 implementation of hotspot.
We know that the JVM is primarily implemented in C + +, and the class inheritance structure for the thread defined by the JVM is as follows:
Class hierarchy
-Thread
-Namedthread
-Vmthread
-Concurrentgcthread
-Workerthread
-Gangworker
-Gctaskthread
-Javathread
-Watcherthread
There is also an important class Osthread not in this inheritance relationship, which is used by the thread class in a combined way
These classes form the threading model of the JVM, the most important of which are the following classes:
Java.lang.Thread: This is a threading class in the Java language, and the instance created by this Java class will map 1:1 to an operating system Osthread
Javathread: The class defined by C + + in the JVM, a Javathread instance represents the instance of Java.lang.Thread in the JVM, maintains the state of the thread, and maintains a pointer to the object Java.lang.Thread created ( OOP). It also maintains a pointer to the corresponding osthread to get the state of the osthread created by the underlying operating system.
Osthread: The classes defined by C + + in the JVM represent an abstraction of the osthread of the underlying operating system in the JVM, which maintains the thread handle handle created by the actual operating system and can obtain the state of the underlying osthread
Vmthread: The class defined in C + + in the JVM, which is independent of the threads created by the user, is the thread that the JVM itself uses for virtual machine operations, such as a GC.
There are two ways to enable users to create threads in the JVM
1. New Java.lang.Thread (). Start ()
2. Use JNI to attach a native thread into the JVM
For new Java.lang.Thread (). Start () This way, only the start () method is called to actually create the thread in the JVM, and the main life-cycle steps are:
1. Create the instance of the corresponding Javathread
2. Create the instance of the corresponding Osthread
3. Create the native thread for the actual underlying operating system
4. Prepare the appropriate JVM state, such as threadlocal storage space allocation, etc.
5. The underlying native thread begins to run, invoking the run () method of the Java.lang.Thread generated object
6. Terminate native thread when the run () method of the Java.lang.Thread generated object returns, or throws an abnormal termination
7. Release the JVM-related thread resources and clear the corresponding Javathread and Osthread
To attach a native thread into the JVM for JNI, the main steps are:
1. Request a connection to the executing JVM instance via JNI call Attachcurrentthread
2. JVM creates the corresponding Javathread and Osthread objects
3. Create the appropriate Java.lang.Thread object
4. Once the Java.lang.Thread object is created, JNI can invoke the Java code
5. After JNI call Detachcurrentthread, JNI disconnects from the JVM instance
6. The JVM clears the corresponding javathread, Osthread, Java.lang.Thread objects
from the JVM's point of view, there are several states of the thread state :
The main states are these 5 kinds:
_thread_new: Newly created thread
_thread_in_java: Running Java code
_THREAD_IN_VM: Code that runs the JVM itself
_thread_in_native: Running the native code
_thread_blocked: The thread is blocked, including waiting for a lock, waiting for a condition, sleep, executing a blocked IO, etc.
from a osthread perspective , the JVM also defines some thread states for external use, such as thread stack information with Jstack output:
More common are:
Runnable: Can be run or running
monitor_wait: Waiting for lock
object_wait: object.wait () is executed after waiting in the condition queue
sleeping: The Thread.Sleep () was executed
from Javathread's point of view , the JVM defines some state for Java thread objects, essentially similar, with a timed_waiting state that represents the state of a timed block.
Finally, let's look at the VM Threads inside the JVM, mainly by several categories:
Vmthread: Performing operations on the JVM itself
Periodic task THREAD:JVM internal thread that performs timed tasks
GC THREADS:GC related threads, such as threads used by a single-threaded/multithreaded GC Collector
Compiler Threads:jit used to dynamically compile the thread
Signal Dispatcher Thread:java interpreter interceptor the thread used to aid the safepoint operation
Specific vmthread of the role, will be in the safepoint of the time to elaborate, write here
Reference: Hotspot JVM thread Management
Talk about the JVM (v) Understanding threads from the JVM perspective