当前位置:网站首页>[interview: concurrent article 28:volatile] orderliness
[interview: concurrent article 28:volatile] orderliness
2022-07-28 01:52:00 【I cream】
【 interview : Concurrent articles 28:volatile】 Orderliness
00. Preface
If you have any questions, please point out , thank .
01. Introduce
What is order
Without changing the results of the program Instructions can be reordered and combined at various stages to achieve instruction level parallelism .
Be careful :
Reorder instructions cannot affect the results
Why do you do this
cpu Treatment process
stay cpu Processing an instruction involves a series of processes , Contains : Fingering decoding Address generation perform Write back , These five steps But these five steps can be staggered , That is, when one instruction is decoded, another instruction can take a finger .
Example 1
x=a;
y=b;

Such implementation efficiency is obviously faster , And when there is no relationship between statements The order of execution can be arbitrary , That is, it is possible to execute y=b Re execution x=a, It doesn't affect the result
Example 2
x=a;
c=x+1;
y=b;
Whether this code can apply the above picture , You can't .
Theoretically, it should correspond to the following figure 
But in fact, this code may be optimized
x=a;
y=b;
c=x+1;
The corresponding picture 
And the reason for that is c=x+1 need x The value of That is, we must wait x Can only be executed after all the instructions of c Instructions , But in the original order y=b It's at the end of That is, he also needs to be in x Instructions can only be run after execution however y=b It has nothing to do with the first two instructions , So optimized Make in x Operation period y It can also run , Improved concurrency , Reduce the situation of running a single instruction .
summary : This case in example 2 is instruction reordering , In the case of single thread, there is no impact , But in the case of multithreading, there may be problems .
02. Instruction sequencing in multithreading
public class Disorder {
private static int x=0,y=0;
private static int a=0,b=0;
// No matter how the two threads are arranged and combined If there is no reordering , It must not exist x=0 And y=0
public static void main(String[] args) throws InterruptedException {
int i=0;
// Endless loop
while (true){
i++;
x=0;y=0;
a=0;b=0;
Thread one= new Thread(()-> {
// There is no dependency a=1 and x=b
a=1;
x=b;
});
Thread other = new Thread(()-> {
// If there is no disorder ,b=1 It must be y=a front
b=1;
y=a;
});
one.start();other.start();
one.join();other.join();
String result = " The first "+i+" Time ("+x+","+y+")";
if(x==0 && y==0){
System.err.println(result);
break;
}else{
System.out.println(result);
}
}
}
public static void shortWait(long interval){
long start = System.nanoTime();
long end;
do{
end=System.nanoTime();
}while (start+interval>=end);
}
}
result
.
.
.
.
The first 181799 Time (0,1)
The first 181800 Time (0,1)
The first 181801 Time (0,1)
The first 181802 Time (0,1)
The first 181803 Time (0,1)
The first 181804 Time (0,0)
explain
We analyze the above code , Consider multithreading , Theoretically x,y There is only 3 Kind of (1,0) (1,1) (0,1), But we found the fourth situation (0,0) It must be x=b stay a=1 Before ,y=a stay b=1 Before , Obviously, in this case, there is only instruction reordering
03. Disable instruction reordering
It's very simple. It's in x,y Add one before volatile, The function is to ensure that x,y The previous code executes before it , for example a=1;x=b; Guarantee a=1 It must be x=b Before execution , Empathy y In the same way , At the same time, the previous variables, including their own values, are synchronized to main memory . At the same time, we need to give a,b with volatile Guarantee a b The visibility of Otherwise, there may be a b Has been updated to main memory But what is read is still local memory
Add : The last paragraph of explanation may not be understood clearly , The specific explanation involves volatile Principle , The next article will talk about .
边栏推荐
- Brushes and brushes
- 学习了循环碰到了编写计算n的阶乘的题目,由此引发了一系列问题,包括一些初学者常见的坑,以及如何简化代码
- Interview question 01.08. Zero matrix
- 简单为美-编程思路
- IIC读写EEFPROM
- For newly installed PIP3, use no module named 'LSB_ Release 'problem
- 暴雪《暗黑破坏神 4》PS5 / PS4 测试版添加到 PlayStation 数据库
- LeetCode第 83 场双周赛
- 2.2 comprehensive application questions - sequence table
- Collection / container
猜你喜欢
随机推荐
爬虫学习的一个综合案例——访问网站
阿虎的故事
三舅的故事
Modify the request path using the streaming API of gateway
Interview question 01.07. rotation matrix
GBase 8c 备份控制函数(四)
处理数据 给数据换名字
暴雪《暗黑破坏神 4》PS5 / PS4 测试版添加到 PlayStation 数据库
了解Shader
Tencent cloud hiflow scene connector
GBase 8c 备份控制函数(一)
Leetcode: 515. Find the maximum value in each tree row
机器学习如何做到疫情可视化——疫情数据分析与预测实战
抓包精灵NetCapture APP抓包教程《齐全》
【样式集合1】tab 栏
集合/容器
总结:Prometheus存储
Stock problems 5 times
【向 Dice Roller 应用添加图片】
Flink 在 讯飞 AI 营销业务的实时数据分析实践









