当前位置:网站首页>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
边栏推荐
- (原创)自定义Drawable
- lvgl 说明3关于lvgl guider的使用
- Lvgl description 3 about the use of lvgl Guide
- 枚举?构造器?面试Demo
- That is, a one-stop live broadcast service with "smooth live broadcast" and full link upgrade
- Eight misunderstandings, broken one by one (final): the cloud is difficult to expand, the customization is poor, and the administrator will lose control?
- lvgl使用demo及说明2
- vim 从嫌弃到依赖(19)——替换
- Lvgl usage demo and instructions 2
- The difference between ArrayList and LinkedList
猜你喜欢
![[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

Matlab tips (19) matrix analysis -- principal component analysis

100%弄明白5种IO模型

Five basic types of redis
![[batch dos-cmd command - summary and summary] - parameters%0,%1,%2,%[0-9],%0-9 in the batch command and batch command parameter position switching command shift, operator% usage in the DOS command](/img/05/19299c47d54d4ede95322b5a923093.png)
[batch dos-cmd command - summary and summary] - parameters%0,%1,%2,%[0-9],%0-9 in the batch command and batch command parameter position switching command shift, operator% usage in the DOS command

SPARQL基础入门练习
![[batch dos-cmd command - summary and summary] - map folder to virtual disk - subst](/img/09/cd12c276392d3465dce1909d0f86a6.png)
[batch dos-cmd command - summary and summary] - map folder to virtual disk - subst

支付宝微信支付业务流程图

100% understanding of 5 IO models

Rust async: SMOL source code analysis -executor
随机推荐
UE5神通--POI解决方案
(原创)自定义Drawable
数字IC-1.9 吃透通信协议中状态机的代码编写套路
Refer to | the computer cannot access the Internet after the hotspot is turned on in win11
100% understanding of 5 IO models
第十一章 信号(一)- 概念
SPARQL basic introductory exercise
Rough reading DS transunet: dual swing transformer u-net for medical image segmentation
[batch dos-cmd command - summary and summary] - map folder to virtual disk - subst
100%弄明白5种IO模型
June 26, 2022 (LC 6100 counts the number of ways to place houses)
Ue5 magic power - POI solution
Oracle uses an SQL to find out which data is not in a table
oracle用一条sql查出哪些数据不在某个表里
SIG associé: toutes les routes mènent à ue5
盲測調查顯示女碼農比男碼農更優秀
[12. maximum continuous non repeating subsequence]
Lvgl description 3 about the use of lvgl Guide
Chapter 11 signal (I) - concept
2022.6.26-----leetcode. seven hundred and ten