当前位置:网站首页>线程运行原理
线程运行原理
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 执行结束后, 会根据返回地址逐步返回, 直到结束
边栏推荐
- 微信小程序rich-text图片宽高自适应的方法介绍(rich-text富文本)
- Safety and food security for teachers and students of the trapped Yingxi middle school
- How to use multiple kindeditor editors on a page and pass values to the server
- uniapp实现点击拨打电话功能
- 2. login and exit function development
- Record the range of data that MySQL update will lock
- JMeter接口测试工具基础— 取样器sampler(二)
- 线程调度的常用方法
- Engine localization adaptation & Reconstruction notes
- 小程序 rich-text中图片点击放大与自适应大小问题
猜你喜欢

leetCode-223: 矩形面积

Machine learning perceptron and k-nearest neighbor

2022 International Symposium on intelligent robots and systems (isoirs 2022)

Thread pool execution process

线程池的执行流程

线程池的状态

How to customize sharing links in wechat for H5 web pages

JMeter接口测试工具基础 — Badboy工具

Machine learning - principal component analysis (PCA)

Leetcode-498: diagonal traversal
随机推荐
p5.js千纸鹤动画背景js特效
Leetcode interview question 01.05: primary editing
23. Opencv——图像拼接项目
numpy. linspace()
Error reading CSV (TSV) file
【资源分享】2022年第五届土木,建筑与环境工程国际会议(ICCAEE 2022)
Learning to organize using kindeditor rich text editor in PHP
线程池的状态
Juul, the American e-cigarette giant, suffered a disaster, and all products were forced off the shelves
Image click enlargement and adaptive size in the applet rich text
JMeter接口测试工具基础 — Badboy工具
Network of test and development - Common Service Protocols
SSM integration
Learn to use PHP to implement unlimited comments and unlimited to secondary comments solutions
Customize the toolbars of the kindeditor editor. Items removes unnecessary toolbars or retains some toolbars
The difference between the sleep () method and the wait () method of a thread
uniapp实现点击拨打电话功能
tf.errors
leetCode-2221: 数组的三角和
np. float32()