当前位置:网站首页>线程运行原理
线程运行原理
2022-06-24 09:47:00 【兀坐晴窗独饮茶】
栈与栈帧
Java Virtual Machine Stacks (Java 虚拟机栈)
- JVM 中由堆、栈、方法区所组成,其中栈内存是给线程使用,
- 每个线程启动后,虚拟机就会为其分配一块栈内存。
- 每个栈由多个栈帧(Frame)组成,对应着每次方法调用时所占用的内存
- 每个线程只能有一个活动栈帧,对应着当前正在执行的那个方法
具体示例如下 :
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);
}
}
当我们运行到主线程method1(10) 时, 可以看到只有主线程在活动

而当我运行到method2(n+1) 位置时 当前的活动栈帧对应着 method1

继续运行直到运行到 method2时可以看到此时有三个栈帧, 活动栈帧是 method2, 所以
- 每个栈由多个栈帧(Frame)组成,对应着每次方法调用时所占用的内存
- 每个线程只能有一个活动栈帧,对应着当前正在执行的那个方法

栈帧图解
代码示例
package cn.knightzz.example.e2.source;
/** * @author 王天赐 * @title: TestFrame * @projectName hm-juc-codes * @description: 线程的栈帧 * @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);
}
}
初始状态如下 :
- 所有的代码加载进方法区, JVM虚拟机为主程序分配栈内存

执行主方法, 创建 main栈帧

局部变量表 :
- 存储局部变量以及方法参数, 对于main函数来说, 局部变量表存储的就是
args args存储的是执行程序的命令信息 :java -jar -Xmx2G后面的参数信息
继续向下执行, 执行到 method1 方法内是 , 此时注意 :
- 程序计数器里记录了此时程序的位置 :
method1(10), 随着继续向下执行, 程序计数器会记录执行的每一行代码 - 返回地址指向方法区里
main函数位置的method1(10)位置 - 局部变量存储了
n和m两个变量, 一个是方法输入参数, 一个是对象 - 注意 : 对象是存储在堆中的

当我们继续向下执行时, 执行到 method2 :
- 返回地址指向
method1中调用method2的位置

当最后一个方法 method2 执行结束后, 会根据返回地址逐步返回, 直到结束
边栏推荐
- Baidu online disk download has been in the process of requesting solutions
- Learn to use the phpstripslush function to remove backslashes
- Younger sister Juan takes you to learn JDBC --- 2-day sprint Day1
- CVPR 2022 oral | NVIDIA proposes an efficient visual transformer network a-vit with adaptive token. The calculation of unimportant tokens can be stopped in advance
- 跨域概述,简单积累
- p5.js千纸鹤动画背景js特效
- dedecms模板文件讲解以及首页标签替换
- Leetcode-1089: replication zero
- How large and medium-sized enterprises build their own monitoring system
- numpy. linspace()
猜你喜欢

How can I solve the problem that the swiper animation animation fails when switching between left and right rotations of the swiper?

uniapp开发微信小程序,显示地图功能,且点击后打开高德或腾讯地图。

Record the range of data that MySQL update will lock

SQL Server AVG函数取整问题

5. dish management business development

uniapp 开发微信公众号,下拉框默认选中列表第一个

Flink集群搭建以及企业级yarn集群搭建

2022年智能机器人与系统国际研讨会(ISoIRS 2022)

线程的 sleep() 方法与 wait() 方法的区别

Appium自动化测试基础 — 移动端测试环境搭建(一)
随机推荐
一群骷髅在飞canvas动画js特效
线程池的执行流程
The great charm of cookies
跨域概述,简单积累
Distributed | how to make "secret calls" with dble
leetCode-面试题 01.05: 一次编辑
Resolved: methods with the same name as their class will not be constructors in
SQL Server AVG函数取整问题
numpy. linspace()
【资源分享】2022年第五届土木,建筑与环境工程国际会议(ICCAEE 2022)
2.登陆退出功能开发
p5.js实现的炫酷交互式动画js特效
解决Deprecated: Methods with the same name as their class will not be constructors in报错方案
2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。
leetCode-1823: 找出游戏的获胜者
p5.js千纸鹤动画背景js特效
卷妹带你学jdbc---2天冲刺Day1
uniapp开发微信小程序,显示地图功能,且点击后打开高德或腾讯地图。
3.员工的增删改查
Difference between package type and basic type