3

java | volatile 原理

 1 year ago
source link: https://benpaodewoniu.github.io/2022/12/24/java140/
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.
neoserver,ios ssh client

java | volatile 原理

2022-12-242022-12-25java进阶多线程volatile

5

volatile 的底层实现是内存屏障。

  • volatile 变量的写指令前会加入写屏障
  • volatile 变量的读指令前会加入读屏障

写屏障保证在该屏障之前的,对共享变量的改动,都同步到主存中

保证在该屏障之后,对共享变量的读取,加载的是主存的最新数据

写屏障会确保指令重排序时,不会将写屏障之前的代码排在写屏障之后

读屏障会确保指令重排序时,不会将读屏障之后的代码排在读屏障之前


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK