当前位置:网站首页>[JUC] concurrent keyword volatile
[JUC] concurrent keyword volatile
2022-07-25 23:31:00 【Seven days at night】
Concurrent keywords you need to know volatile
1. keyword volatile What is it? ?
volatile yes Java Lightweight synchronization mechanism provided by virtual machine .
Its three characteristics are :
1> visibility
2> There is no guarantee of atomicity
3> Prohibit command rearrangement
The detailed explanation of specific features will not be repeated here ;
1.1 visibility
volatile The variable visibility of decoration is mainly reflected in the following aspects :
- Any thread pair volatile The write of modified variables will be synchronized to the main memory immediately ;
- Other threads will read the latest value when reading this variable , That is to say, the latest content ;

1.2 There is no guarantee of atomicity
This happens in the case of concurrency :
1.3 Prohibit command rearrangement
When the computer is executing the program , To improve performance , Compilers and processors reorder instructions .
1. Single threaded environment , Reordering can ensure that the final result of the program is consistent with the result of sequential execution
2. The processor processing reordering must consider the data dependency between instructions ( Such as b rely on a, Can't be b Sort in a Before execution )
3. During multithread scheduling , Due to reordering , The variables of two threads cannot guarantee consistency

3. keyword volatile What scenarios are applicable to ?
volatile It can be understood as lightweight synchronized, Most suitable for a thread to write , When multiple threads read .
We are java.util.concurrent.atomic Found in the atomic class of , Use volatile Keywords are atomic ;
4. Suggestions for practical use
If you use volatile Modifying variables , Be sure to pay attention to volatile Whether the feature of is applicable in the business scenario .
边栏推荐
- Take away applet with main version of traffic / repair to add main access function of traffic
- Inheritance (the child constructor inherits the attributes in the parent constructor)
- Call Gaode map -- address is converted into longitude and latitude
- [test technology performance test LoadRunner] detailed explanation of common functions of LoadRunner
- PHP wechat scan code, follow official account and authorize login source code
- 策略模式_
- 学习探索-3d轮播卡片
- serialization and deserialization
- Source code of wechat applet for discerning flowers and plants / source code of wechat applet for discerning plants
- @Import
猜你喜欢

E-commerce RPA, a magic weapon to promote easy entry

生成随机数random学习之uniform_int_distribution,uniform_real_distribution

Anti shake and throttling

Discuz magazine / news report template (jeavi_line) utf8 GBK / DZ template download

Inheritance (the child constructor inherits the attributes in the parent constructor)

ES6 syntax (difference between let, const, VaR, deconstruction assignment, arrow function, residual parameters, extension method of array)

数组中重复的数字
![[nodejs] nodejs create a simple server](/img/00/7ab5629bf67777da21b41f44665880.png)
[nodejs] nodejs create a simple server

serialization and deserialization

numeric学习之iota,accumulate
随机推荐
多模态——Deep Multi-Modal Sets
OASYS system of code audit
Constructors and prototypes
新手开户选择哪个券商公司好呢?安全吗
About the foundation of fetch
MVVM model
@Autowired注解 required属性
【代码案例】博客页面设计(附完整源码)
Serialize common default values and column parameters
Deep and shallow copies
File contains vulnerability
Promise asynchronous callback function
Duplicate numbers in array
[QNX Hypervisor 2.2用户手册]9.8 load
How does Navicat modify the language (Chinese or English)?
策略模式_
[code case] blog page design (with complete source code)
Implementation of date class
物理防火墙是什么?有什么作用?
BI 系统中为什么会有很多快照表?