In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how the JVM thread changes". In the daily operation, I believe that many people have doubts about the change of the JVM thread. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how the JVM thread changes" Next, please follow the editor to study!
The jvm thread maintains the state of the thread. New,running,waiting,timed waiting,blocked,terminated . Thread status is one of the above when we look at it through tools such as jstack. Jvm itself is an abstraction, so let's start from a typical way to track and see how state changes are made within jvm.
Sleep method to get public static native void sleep (long millis) throws InterruptedException
Sleep is a native method, and we follow through the jvm source (the source code comes from openjdk11). According to the jni specification, we find the corresponding declaration by registering the package name or jni.
Static JNINativeMethod methods [] = {
{"start0", "() V", (void *) & JVM_StartThread}
{"stop0", "(" OBJ ") V", (void *) & JVM_StopThread}
{"isAlive", "() Z", (void *) & JVM_IsThreadAlive}
{"suspend0", "() V", (void *) & JVM_SuspendThread}
{"resume0", "() V", (void *) & JVM_ResumeThread}
{"setPriority0", "(I) V", (void *) & JVM_SetThreadPriority}
{"yield", "() V", (void *) & JVM_Yield}
{"sleep", "(J) V", (void *) & JVM_Sleep}
{"currentThread", "()" THD, (void *) & JVM_CurrentThread}
{"countStackFrames", () I ", (void *) & JVM_CountStackFrames}
{"interrupt0", "() V", (void *) & JVM_Interrupt}
{"isInterrupted", "(Z) Z", (void *) & JVM_IsInterrupted}
{"holdsLock", "(" OBJ ") Z", (void *) & JVM_HoldsLock}
{"getThreads", "() [" THD, (void *) & JVM_GetAllThreads} "
{"dumpThreads", "([" THD ") [[" STE, (void *) & JVM_DumpThreads} "
{"setNativeName", "(" STR ") V", (void *) & JVM_SetNativeThreadName}
}
The next thing we're going to follow is JVM_Sleep. Let's analyze this method bit by bit. First of all, there is a macro definition.
JVM_ENTRY (void, JVM_Sleep (JNIEnv* env, jclass threadClass, jlong millis))
Let's expand JVM_ENTRY.
# define JVM_ENTRY (result_type, header)\
Extern "C" {\
Result_type JNICALL header {\
JavaThread* thread=JavaThread::thread_from_jni_environment (env);\
ThreadInVMfromNative _ _ tiv (thread);\
Debug_only (VMNativeEntryWrapper _ _ vew;)\
VM_ENTRY_BASE (result_type, header, thread)
In ThreadInVMfromNative, a change in thread state occurs.
Class ThreadInVMfromNative: public ThreadStateTransition {
Public:
ThreadInVMfromNative (JavaThread* thread): ThreadStateTransition (thread) {
Trans_from_native (_ thread_in_vm)
}
~ ThreadInVMfromNative () {
Trans_and_fence (_ thread_in_vm, _ thread_in_native)
}
}
In the constructor, the * * thread- > thread_state () * * state of JavaThread is changed to _ thread_in_vm. The state is changed to _ thread_in_native in the destructor. The _ _ tiv here is a local object, and destructing is triggered only when the stack is destroyed, which means that the conversion to _ thread_in_native here is only an instant. After unfolding the head, we will continue to look back.
JavaThreadSleepState jtss (thread)
In this construction method. Change the state of * * java_thread- > threadObj () * * to
Java_lang_Thread::SLEEPING
Static void set_thread_status (JavaThread* java_thread
Java_lang_Thread::ThreadStatus state) {
Java_lang_Thread::set_thread_status (java_thread- > threadObj (), state)
}
This corresponds to the state of the thread of java. Set the status of * * thread- > osthread () * * to sleep directly when you go down.
ThreadState old_state = thread- > osthread ()-> get_state ()
Thread- > osthread ()-> set_state (SLEEPING)
The status is all set here.
State carding
From the above code, we can find that the core is the object of JavaThread, which itself represents the thread state in jvm. Identifies whether the thread is in vm or in java or native. The specific status is as follows
Enum JavaThreadState {
_ thread_uninitialized = 0, / / should never happen (missing initialization)
_ thread_new = 2, / / just starting up, i.e., in process of being initialized
_ thread_new_trans = 3, / / corresponding transition state (not used, included for completness)
_ thread_in_native = 4, / / running in native code
_ thread_in_native_trans = 5, / / corresponding transition state
_ thread_in_vm = 6, / / running in VM
_ thread_in_vm_trans = 7, / / corresponding transition state
_ thread_in_Java = 8, / / running in Java or in stub code
_ thread_in_Java_trans = 9, / / corresponding transition state (not used, included for completness)
_ thread_blocked = 10, / / blocked in vm
_ thread_blocked_trans = 11, / / corresponding transition state
_ thread_max_state = 12 / / maximum thread state+1-used for statistics allocation
}
This class is also a reference to the java thread state. It is java_thread- > threadObj (). This corresponds to the thread state of java, which is also the state we see in jstack.
"main" # 1 prio=5 os_prio=31 tid=0x00007fee9b809000 nid=0xe03 waiting on condition [0x0000700008d65000]
Java.lang.Thread.State: TIMED_WAITING (sleeping)
At java.lang.Thread.sleep (Native Method)
At com.company.Sleep.main (Sleep.java:7)
Why the output is timed_waiting (sleep), this is mainly because of formatting the output.
If (status = = THREAD_STATUS_NEW) {
Return "NEW"
} else if (status = = THREAD_STATUS_RUNNABLE) {
Return "RUNNABLE"
} else if (status = = THREAD_STATUS_SLEEPING) {
Return "TIMED_WAITING (sleeping)"
} else if (status = = THREAD_STATUS_IN_OBJECT_WAIT) {
Return "WAITING (on object monitor)"
} else if (status = = THREAD_STATUS_IN_OBJECT_WAIT_TIMED) {
Return "TIMED_WAITING (on object monitor)"
} else if (status = = THREAD_STATUS_PARKED) {
Return "WAITING (parking)"
} else if (status = = THREAD_STATUS_PARKED_TIMED) {
Return "TIMED_WAITING (parking)"
} else if (status = = THREAD_STATUS_BLOCKED_ON_MONITOR_ENTER) {
Return "BLOCKED (on object monitor)"
} else if (status = = THREAD_STATUS_TERMINATED) {
Return "TERMINATED"
}
Return "UNKNOWN"
}
When the state is formatted, the sleeping is classified as TIMED_WAITING (sleeping). At the same time, Java Thread also holds the system thread thread- > osthread ().
At this point, the study of "what is the change of JVM threads" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.