当前位置:网站首页>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
边栏推荐
- June 26, 2022 (LC 6100 counts the number of ways to place houses)
- [daily practice] realization of product card animation effect
- 今日3大面试Demo[Integer ASCII 类关系]
- 静态代码块Vs构造代码块
- L'enquête en aveugle a montré que les femmes étaient meilleures que les hommes.
- SPARQL基础入门练习
- Correctly understand MySQL mvcc
- The difference between ArrayList and LinkedList
- SPARQL basic introductory exercise
- PayPal account has been massively frozen! How can cross-border sellers help themselves?
猜你喜欢

(original) custom drawable

關聯GIS:條條道路通UE5城

oracle用一条sql查出哪些数据不在某个表里

(note) Anaconda navigator flashback solution

oracle怎样将字符串转为多行

准备好迁移上云了?请收下这份迁移步骤清单

vim 从嫌弃到依赖(19)——替换

Read datasets iteratively with xgboost

June 26, 2022 (LC 6100 counts the number of ways to place houses)

Analysis of orthofinder lineal homologous proteins and result processing
随机推荐
Code source AQS sous - jacent pour la programmation simultanée juc
Linux下Redis的安装
SPARQL基础入门练习
L'enquête en aveugle a montré que les femmes étaient meilleures que les hommes.
PayPal账户遭大规模冻结!跨境卖家如何自救?
SIG associé: toutes les routes mènent à ue5
After working in a large factory for ten years with an annual salary of 400000 yuan, I was suddenly laid off. If the company wanted to abandon you, it wouldn't leave any kindness
[13. number and bit operation of 1 in binary]
JS EventListener
If xn > 0 and X (n+1) /xn > 1-1/n (n=1,2,...), Prove that the series Σ xn diverges
[daily practice] realization of product card animation effect
力扣84柱状图中最大的矩形
[10. difference]
粗读DS-TransUNet: Dual Swin Transformer U-Net for Medical Image Segmentation
vim 从嫌弃到依赖(19)——替换
关于el-date-picker点击清空参数变为null的问题
三道基础面试题总结
内部类~锁~访问修饰符
MATLAB小技巧(19)矩阵分析--主成分分析
Redis五种基本类型