当前位置:网站首页>线程运行原理
线程运行原理
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 执行结束后, 会根据返回地址逐步返回, 直到结束
边栏推荐
- leetCode-1051: 高度检查器
- Development of anti fleeing marketing software for health products
- uniapp实现点击拨打电话功能
- SSM integration
- Customize the toolbars of the kindeditor editor. Items removes unnecessary toolbars or retains some toolbars
- tf. errors
- 美国电子烟巨头 Juul 遭遇灭顶之灾,所有产品强制下架
- 一群骷髅在飞canvas动画js特效
- 4.分类管理业务开发
- leetCode-1823: 找出游戏的获胜者
猜你喜欢

SQL Server AVG函数取整问题

美国电子烟巨头 Juul 遭遇灭顶之灾,所有产品强制下架

正规方程、、、

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

上升的气泡canvas破碎动画js特效

Six states of threads

Troubleshooting steps for Oracle pool connection request timeout

一群骷髅在飞canvas动画js特效

How to customize sharing links in wechat for H5 web pages

使用swiper左右轮播切换时,Swiper Animate的动画失效,怎么解决?
随机推荐
2022年智能机器人与系统国际研讨会(ISoIRS 2022)
Using pandas to read SQL server data table
Learning to organize using kindeditor rich text editor in PHP
uniapp实现禁止video拖拽快进
跨域概述,简单积累
4.分类管理业务开发
解决Deprecated: Methods with the same name as their class will not be constructors in报错方案
Troubleshooting steps for Oracle pool connection request timeout
What are the characteristics of EDI local deployment and cloud hosting solutions?
Distributed | how to make "secret calls" with dble
tf.contrib.layers.batch_norm
Image click enlargement and adaptive size in the applet rich text
web网站开发,图片懒加载
如何在一个页面上使用多个KindEditor编辑器并将值传递到服务器端
Leetcode-2221: triangular sum of arrays
numpy.linspace()
dedecms模板文件讲解以及首页标签替换
Thread pool execution process
卷妹带你学jdbc---2天冲刺Day1
tf. errors