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/01 Report--
This article will explain in detail the example analysis of Java thread running. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Stack and stack frame
JVM consists of heap, stack and method area, in which stack memory is allocated to threads. After each thread starts, the virtual machine allocates a stack memory to it.
Each stack consists of multiple stack frames, corresponding to the memory occupied by each method call
'each thread can have only one active stack frame, corresponding to the currently executing method
Public class Main {public static void main (String [] args) {method1 (10);} private static void method1 (int x) {int y = x + 1; Object m = method2 (); System.out.println (m);} private static Object method2 () {Object n = new Object (); return n;}}
First, the class is loaded, and the class information enters the method area.
After the class is loaded, jvm starts a main thread whose thread is main, allocates a stack memory to the thread, and allocates it to the task scheduler for execution. When the time slice is allocated, it allocates a stack frame memory to the main method.
The program counter stores the position of the statement to be executed on the next line, executes the method1 statement to the main method, calls the method1 method, and assigns stack frames to the method1 method
At the same time, the mehtod2 method is called in method1, and jvm allocates stack frames to method2.
After the execution of method 2, release the stack frame memory of method2 and execute the code at the return address of method2
The subsequent execution is not repeated, but as the execution of each method ends, it goes out of the stack in turn.
Context switching of threads
Cpu no longer executes the current thread and instead executes the code of another thread for the following reasons
The thread runs out of cpu time slices
Garbage collection
Garbage collection stops all currently working threads and executes GC threads
Threads with higher priority need to run
The thread calls methods such as sleep,yield,wait,join,park,synchronized, lock, etc.
When Context Switch occurs, the operating system needs to save the state of the current thread and restore the state of another thread. The corresponding concept in Java is the program counter, which records the execution address of a jvm execution, which is private to the thread.
The state includes program counters, information of each stack frame in the virtual machine stack, such as local variables, Operand stack, return address, etc.
Frequent occurrence of Context Switch can affect performance
This is the end of this article on "sample analysis of Java thread running". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.