当前位置:网站首页>After 4 years of developing two-sided meituan, we finally lost: the interview question of volatile keyword function and principle
After 4 years of developing two-sided meituan, we finally lost: the interview question of volatile keyword function and principle
2022-07-19 03:02:00 【Tom bomb architecture】
a 6 A little partner with years of working experience , I was asked when I was in a certain place “volatile” keyword . then , No, then …
Again , And one more 4 Year's little friend , I was also asked about the interview in a certain group “volatile keyword “. then , There was no more …
volatile Keywords are Java An important keyword in concurrent programming , This question is really asked at the bottom . Is it true that big factories are so busy now ? What about today , Let me share my understanding , I hope I can help you .
in addition , I prepared a copy of 500 page PDF Supporting documents for interview question analysis ,
How to get ? :
Scan the QR code at the bottom of the article to get !
1、volatile The role of
volatile Key words? , It does two things :

The first 1 individual , It can ensure the visibility of shared variables in a multithreaded environment .
The first 2 individual , It can be shielded in a multithreaded environment CPU The command rearrangement of .
below , Let me give you a detailed introduction to visibility and shielding instruction rearrangement and volatile How it works .
2、 Principle analysis
Let's first look at the visibility of variables , Simply speaking , It refers to the modification of shared variables by a thread , Other threads can immediately see the modified value . In fact, this visibility problem , I think it is essentially caused by the following two aspects .
First of all ,CPU The cache of . stay CPU A three-level cache is designed to solve CPU Operational efficiency and memory IO The efficiency problem , But there are cache consistency problems , In the case of multi-threaded parallel execution , Cache consistency can lead to visibility problems .

therefore , For added volatile Keyword decorated shared variables ,JVM A virtual opportunity is automatically added #Lock Assembly instruction , This instruction will be based on CPU The model automatically adds a bus lock or / Buffer lock .
Let me briefly introduce these two kinds of locks :
The bus lock is locked CPU Front end bus , As a result, only one thread can communicate with memory at the same time , This avoids the visibility caused by multithreading concurrency .
Cache lock is the optimization of bus lock , Because the bus lock caused CPU The use efficiency of has decreased significantly , So the cache lock is only for CPU Lock the target data in the L3 cache , Cache lock is used MESI Cache consistency .
then , Is to shield instruction rearrangement , It means shielding CPU Instruction reordering . It means in a multithreaded environment ,CPU The writing order of instructions is inconsistent with the execution order , This leads to visibility problems , In order to improve CPU Utilization ratio ,CPU Introduced StoreBuffer Mechanism , And this kind of optimization mechanism will lead to CPU To execute in disorder . Of course, in order to avoid such problems ,CPU Provides memory barrier instructions , Upper layer applications can insert memory barriers in appropriate places to avoid CPU Command reordering problem . and volatile It is to set a memory barrier to prevent instruction rearrangement .
volatile The implementation principle of memory barrier is mainly analyzed from the following two aspects :
The first 1 Yes :volatile Two memory barriers are added before and after the variable write operation , To ensure that the previous write instructions and the subsequent read instructions are in order , As shown in the figure :

The first 2 Yes :volatile Insert two instructions after the read operation of the variable , Prevent subsequent read and write reordering ., As shown in the figure :

volatile In fact, it can be regarded as lightweight synchronized, Although I say volatile Atomicity is not guaranteed , But if the operation under multithreading itself is atomic ( For example, assignment operation ), So use volatile Better than synchronized. So that's me right volatile Keyword understanding .
Concurrent programming is a field that every programmer must master , It covers the design ideas 、 And concurrency problems 、 And as a concurrency tool , Are very worthy of in-depth study .
Last , I sorted all the videos I shared in previous periods into 1 Share 20W Document of words , I hope this can improve the passing rate of all fans

I was delayed by programming Tom, Only dry goods without water ! Your support is my biggest motivation ! Pay attention to me , The interview is no longer difficult !
Complete interview materials and answers, as well as PDF file :
Scan the QR code below to get !
↓ ↓ ↓
边栏推荐
猜你喜欢
随机推荐
【单片机仿真】(十一)指令系统逻辑运算指令 — 逻辑与指令ANL、逻辑或指令ORL
05_服务调用Ribbon
當你在Linux系統中編譯安裝MySQL數據庫卡住了怎麼辦?
[single chip microcomputer simulation] (XI) instruction system logic operation instruction - logic and instruction anl, logic or instruction ORL
Configure VLAN and use OSPF protocol for layer 3 switches
[MCU simulation] (VII) addressing mode - bit addressing
2、AsyncTool框架实战使用
[single chip microcomputer simulation] (XII) instruction system logic operation instruction - logic XOR instruction XRL, accumulator clear 0 and reverse instruction
【单片机仿真】(七)寻址方式 — 位寻址
MySQL数据库中的事务和存储引擎
[MCU simulation] (II) keil installation tutorial
ncnn Mat矩阵类
Fiddler抓包
Redis' simple dynamic string SDS
When the drain operation is performed on the work node, the number of pod copies is protected through the PDB
Elk log analysis system
仿射变换实现
About XML file (VI) - the difference between JSON and XML file
Systick timer basic learning and hand tearing code
Go语言 实现发送短信验证码 并登录
![[redis] what is progressive rehash](/img/99/0b2c81e55a70c41de245612996d1a2.png)







