当前位置:网站首页>JVM运行时数据区之程序计数器
JVM运行时数据区之程序计数器
2022-07-06 09:30:00 【机智的爆爆哥】
1. 介绍
程序计数器又叫PC寄存器,全称为 Program Counter Register,直译过来的,可以看做是真实物理寄存器的一种抽象模拟。

它主要的作用是存储下一条指令的地址,可以看到下图,从5的位置得到对应的操作指令,放入PC寄存器中存取,之后执行引擎会读取,将操作指令翻译成机器指令,执行一系列的加减乘除之类的操作。

回顾一下,就如下图所示。

2. 特点
它还有以下的一些特点。
- 占用的内存非常小,几乎可以忽略不计,运行速度也很快。
- 线程私有,每个线程都有一块,不会有线程安全问题
- 一个线程对应一个方法,程序计数器会存储线程中运行方法的指令地址,但如果执行的是本地方法,指令地址为underfine,无法记录,因为他只记录Java方法
- 分支 循环 跳转等都依赖于这个计数器
- 在JVM规范中唯一不会OOM的区域
3.CPU时间片
我们来讲讲CPU时间片的概念,这是CPU分配给各个程序执行的时间。
对于单核CPU,没有并行的概念,但是因为CPU时间片的切换,会给我们一种并发的假象,即多个程序同时在运行,因为每个程序可能只运行10ns的时间,来回切换,但本质上其实还是单线程的操作。
但对于多核CPU,多个程序会分配给不同的核心进行处理,同一时刻不同线程执行不同的工作,但对于每个核心,会有并发操作,即各个线程交替,
4. 两个问题
PC寄存器存储字节码指令地址有什么用?
在多线程的情况下,执行一个方法会有多个线程的参与,所以要记录指令地址,否则切换的线程要从头再执行,那肯定是不对的。
就拿4*100米接力来说,接力的那个人在100米,200米,300米的地方等着,而不是从头开始比赛,接力的位置相当于指令地址。
PC寄存器为什么要线程私有?

假设PC寄存器是线程共享的,那么会带来什么问题呢,假设三个线程执行的是三个不同的方法。
首先线程1执行到指令5了,开始切换,线程2执行最终执行到指令7,会将指令5的位置覆盖掉,等线程123都执行了一遍,线程1再开始执行,就是从指令17开始执行了,可是原先它才执行到指令5,但没有记录,肯定会造成程序的混乱,所以程序计数器不得不是线程私有的。
每日分享
如果你曾经也嫌弃过父母没本事,没眼界,意味着你已经走出了父母的局限,而这是他们倾尽一切想看到的局面。
边栏推荐
- 原型链继承
- 数据传送指令
- [unsolved]7-14 calculation diagram
- Fdog series (V): use QT to imitate QQ to realize login interface to main interface, function chapter.
- Ruoyi-Cloud 踩坑的BUG
- koa中间件
- 唯有学C不负众望 TOP1环境配置
- IDEA断点调试技巧,多张动图包教包会。
- 7-12 inventory code base
- Yao BanZhi and his team came together, and the competition experts gathered together. What fairy programming competition is this?
猜你喜欢

Mongodb learning notes

一个数10年工作经验的微服务架构老师的简历

arithmetic operation

8086 CPU internal structure

J'ai traversé le chemin le plus fou, le circuit cérébral d'un programmeur de saut d'octets

Fdog series (I): think about it. It's better to write a chat software. Then start with the imitation QQ registration page.

Fdog series (VI): use QT to communicate between the client and the client through the server (less information, recommended Collection)

Fdog series (V): use QT to imitate QQ to realize login interface to main interface, function chapter.

~84 form supplement

MySQL string function
随机推荐
汇编语言段定义
控制转移指令
Introduction to microservices
Shell_ 07_ Functions and regular expressions
IDEA断点调试技巧,多张动图包教包会。
Saw local status change event StatusChangeEvent [timestamp=1644048792587, current=DOWN, previous=UP]
Usage of insert() in vector
The QT program compiled on CentOS lacks a MySQL driven solution
8086 CPU internal structure
TypeScript基本操作
How to configure hosts when setting up Eureka
[graduation project] QT from introduction to practice: realize imitation of QQ communication, which is also the last blog post in school.
Solr standalone installation
Some instructions on whether to call destructor when QT window closes and application stops
~78 radial gradient
这116名学生,用3天时间复刻了字节跳动内部真实技术项目
Continue and break jump out of multiple loops
After the subscript is used to assign a value to the string type, the cout output variable is empty.
数据传送指令
Solr new core