2
java | 有序性
source link: https://benpaodewoniu.github.io/2022/12/24/java139/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
- 有序性
- 保证指令不会受
CPU
指令并行优化的影响
- 保证指令不会受
i = ...
j = ...
在多线程下,指令可能会发生重排变成
j = ...
i = ...
因为多线程是并行的,所以,指令执行顺序是不确定的。
重排序解决
使用 volatile
进行修饰。
volatile int i = 0;
j = ...
c = ...
d = ...
i = ...
volatile
修饰的变量之前的执行顺序都保持不变。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK