当前位置:网站首页>See how much volatile you know
See how much volatile you know
2022-06-27 08:31:00 【C_ x_ three hundred and thirty】
For the sake of running speed ,java The compiler will put frequently accessed variables into the cache ( Strictly speaking, it should be working memory ) in , Read variables are read from the cache . But in multithreaded programming , There may be inconsistencies between the values in memory and the values in the cache .volatile Used to limit that variables can only be read from memory , Ensure that for all threads , The values are consistent . however volatile Atomicity is not guaranteed , You can't guarantee thread safety
volatile Keyword pair any single volatile The reading and writing of variables can ensure atomicity , But similar to volatile++ This kind of compound operation cannot guarantee atomicity . If you need to guarantee atomicity for this composite operation , It is best to synchronized keyword . namely synchronized Guarantee three characteristics , Atomicity , Orderliness , visibility ,volatile To ensure order , visibility , Atomicity is not guaranteed .
in general : volatile The characteristics of are
Orderliness , visibility
however
newly added
Once a shared variable ( Member variables of class 、 Static member variable of class ) By volatile After retouching , So there are two levels of semantics :1) It ensures the visibility of different threads when they operate on this variable , That is, a thread changes the value of a variable , This new value is immediately visible to other threads .
2) Disables instruction reordering .
volatile Only guarantees are provided when accessing the variable , Every time I read the latest value from memory , Registers are not used to cache this value —— Read from memory every time .
And the modification of this variable ,volatile There is no guarantee of atomicity .
Due to timely updates , It is likely to cause another thread to access the latest variable value , Unable to jump out of the loop
Under multithreading, the counter must be protected by lock
边栏推荐
- SQL Sever column name or number of supplied values does not match the table definition
- ServletConfig and ServletContext
- 100%弄明白5种IO模型
- Modify the contents of /etc/crontab file directly, and the scheduled task will not take effect
- How much do you know about the cause of amplifier distortion?
- lvgl使用demo及说明2
- [c++ primer notes] Chapter 4 expression
- [12. maximum continuous non repeating subsequence]
- [10. difference]
- Lvgl usage demo and instructions 2
猜你喜欢

orthofinder直系同源蛋白分析及结果处理

並發編程JUC的AQS底層源碼

多网络设备存在时,如何配置其上网优先级?

vim 从嫌弃到依赖(20)——global 命令

MySQL lock details

JVM常见的垃圾收集器

"Short video" Linxia fire rescue detachment carries out fire safety training

IO管脚配置和pinctrl驱动

Helix QAC更新至2022.1版本,将持续提供高标准合规覆盖率
![[batch dos-cmd command - summary and summary] - environment variables, path variables, search file location related instructions - set, path, where, what if there are spaces in the path parameters of](/img/ca/0b96294336810187e833429fcea5bf.png)
[batch dos-cmd command - summary and summary] - environment variables, path variables, search file location related instructions - set, path, where, what if there are spaces in the path parameters of
随机推荐
JVM层次上的对象的创建过程和内存布局
C# 解决使用SQLite 的相对路径问题
参考 | Win11 开启热点之后电脑不能上网
That is, a one-stop live broadcast service with "smooth live broadcast" and full link upgrade
Rust async: SMOL source code analysis -executor
粗读DS-TransUNet: Dual Swin Transformer U-Net for Medical Image Segmentation
[batch dos-cmd command - summary and summary] - output / display command - echo
MATLAB小技巧(18)矩阵分析--熵权法
Redis transactions
March into machine learning -- Preface
Associated GIS: all roads lead to ue5 City
LVGL GUI GUIDER移植代码到STM32
我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
内部类~锁~访问修饰符
Lvgl description 3 about the use of lvgl Guide
webrtc入门:12.Kurento下的RtpEndpoint和WebrtcEndpoint
DV scroll board width of datav rotation table component
Redis五种基本类型
VIM from dislike to dependence (20) -- global command
Redis主从复制以及哨兵模式