当前位置:网站首页>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
边栏推荐
- Install Jenkins
- DataV轮播表组件dv-scroll-board宽度问题
- The difference between ArrayList and LinkedList
- [11. two dimensional difference]
- Analysis of key technologies for live broadcast pain points -- second opening, clarity and fluency of the first frame
- [daily practice] realization of product card animation effect
- Creation process and memory layout of objects at JVM level
- 数字IC-1.9 吃透通信协议中状态机的代码编写套路
- Blind survey shows that female code farmers are better than male code farmers
- MySQL环境变量配置的教程
猜你喜欢
随机推荐
[batch dos-cmd command - summary and summary] - map folder to virtual disk - subst
Mysql事务中MVCC理解超简单
MySQL index details
JVM常见的垃圾收集器
About the problem that the El date picker Click to clear the parameter and make it null
参考 | 升级 Win11 移动热点开不了或者开了连不上
[10. difference]
Lvgl GUI guide porting code to stm32
CLassLoader
IO管脚配置和pinctrl驱动
JVM层次上的对象的创建过程和内存布局
Reference | upgrade win11 mobile hotspot can not be opened or connected
Analysis of orthofinder lineal homologous proteins and result processing
ArrayList和LinkedList的区别
June 26, 2022 (LC 6100 counts the number of ways to place houses)
lvgl 说明3关于lvgl guider的使用
今日3大面试Demo[Integer ASCII 类关系]
oracle用一条sql查出哪些数据不在某个表里
How Oracle converts strings to multiple lines
Ue5 magic power - POI solution









