当前位置:网站首页>Thread operation principle
Thread operation principle
2022-06-24 10:33:00 【Sit at a sunny window and drink tea alone】
Stack and stack frame
Java Virtual Machine Stacks (Java Virtual machine stack )
- JVM Zhongyou Pile up 、 Stack 、 Method area The composition of , Stack memory is used by threads ,
- After each thread starts , The virtual machine will allocate a piece of stack memory .
- Each stack consists of multiple stack frames (Frame) form , It corresponds to the memory occupied by each method call
- Each thread can only have one active stack frame , Corresponds to the currently executing method
Specific examples are as follows :
package cn.knightzz.example.e2.source;
public class TestFrame {
public static void method1(int n) {
Object m = method2(n + 1);
System.out.println("m = " + m);
}
public static Object method2(Object n) {
return n;
}
public static void main(String[] args) {
method1(10);
}
}
When we run to the main thread method1(10) when , You can see that only the main thread is active

And when I run to method2(n+1) When in position The current active stack frame corresponds to method1

Continue running until method2 You can see that there are three stack frames at this time , Active stack frame is method2, therefore
- Each stack consists of multiple stack frames (Frame) form , It corresponds to the memory occupied by each method call
- Each thread can only have one active stack frame , Corresponds to the currently executing method

Stack frame diagram
Code example
package cn.knightzz.example.e2.source;
/** * @author Wang Tianci * @title: TestFrame * @projectName hm-juc-codes * @description: Thread stack frame * @website <a href="http://knightzz.cn/">http://knightzz.cn/</a> * @github <a href="https://github.com/knightzz1998">https://github.com/knightzz1998</a> * @create: 2022-06-15 22:18 */
public class TestFrame {
public static void method1(int n) {
Object m = method2(n + 1);
System.out.println("m = " + m);
}
public static Object method2(Object n) {
return n;
}
public static void main(String[] args) {
method1(10);
}
}
The initial state is as follows :
- All the code is loaded into the method area , JVM Virtual machine allocates stack memory for main program

Execute main method , establish main Stack frame

Local variable table :
- Store local variables and method parameters , about main In terms of functions , The local variable table stores
args argsWhat is stored is the command information of the executing program :java -jar -Xmx2GThe following parameter information
Keep going down , Execute to method1 In the method , Note that at this time :
- The program counter records the position of the program at this time :
method1(10), As you continue down , The program counter records every line of code executed - The return address points to the method area
mainFunction positionmethod1(10)Location - Local variables are stored
nandmTwo variables , One is method input parameters , One is the object - Be careful : Objects are stored in the heap

As we continue down , Execute to method2 :
- The return address points to
method1Call inmethod2The location of

When the last method method2 After execution , It will return step by step according to the return address , Until the end
边栏推荐
- 【JS逆向分享】某个网站社区信息
- JMeter interface test tool foundation - sampler (II)
- 2022 International Symposium on intelligent robots and systems (isoirs 2022)
- Leetcode-1089: replication zero
- 2022 the most complete and detailed JMeter interface test tutorial and detailed interface test process in the whole network - JMeter test plan component (thread < user >)
- HBuilder制作英雄皮肤抽奖小游戏
- A method to solve the self-adaptive width and height of the internal picture of rich text label in wechat applet
- Outils de capture de paquets
- 使用swiper左右轮播切换时,Swiper Animate的动画失效,怎么解决?
- 24. 图像拼接大作业
猜你喜欢

Spark submission parameter -- use of files

2022全网最全最细的jmeter接口测试教程以及接口测试流程详解— JMeter测试计划元件(线程<用户>)

1.项目环境搭建

Normal equation

Flink checkPoint和SavePoint

Machine learning perceptron and k-nearest neighbor

Leetcode-1823: find the winner of the game

解决DBeaver SQL Client 连接phoenix查询超时

Leetcode - 498: traversée diagonale

charles抓包工具使用教程
随机推荐
Leetcode-1051: height checker
跨域概述,简单积累
Learn to use PHP to implement unlimited comments and unlimited to secondary comments solutions
機械臂速成小指南(二):機械臂的應用
numpy. logical_ and()
283.移动零
3. addition, deletion, modification and query of employees
Solve the timeout of Phoenix query of dbeaver SQL client connection
学习整理在php中使用KindEditor富文本编辑器
抓包工具charles實踐分享
Leetcode interview question 01.05: primary editing
线程运行原理
pycharm快捷键大全
解决DBeaver SQL Client 连接phoenix查询超时
2. login and exit function development
Web site development, lazy image loading
uniapp实现禁止video拖拽快进
Niuke-top101-bm29
4. classification management business development
多线程的应用 - 提升效率