当前位置:网站首页>[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 .
边栏推荐
- 图的遍历-DFS,BFS(代码详解)
- 152. 乘积最大子数组-动态规划
- Taobao Search case
- POI特效 市场调研
- 生成随机数random学习之uniform_int_distribution,uniform_real_distribution
- [testing technology automated testing pytest] basic summary of pytest
- Why are there many snapshot tables in the BI system?
- [QNX Hypervisor 2.2用户手册]9.6 gdb
- Serialize operator
- The difference between MySQL clustered index and non clustered index
猜你喜欢

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

学习探索-3d轮播卡片

Enterprise level inventory management system of code audit

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

Discuz atmosphere game style template / imitation lol hero League game DZ game template GBK

Family relationship calculator wechat applet source code

Firewall command simple operation

Learning exploration - waves

Regular expression (user name form verification / verification of landline number / regular replacement)

行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化
随机推荐
PHP wechat scan code, follow official account and authorize login source code
Summary of kotlin common knowledge points
Same origin strategy and cross domain
Idea sets get and set templates to solve the naming problem of boolean type fields
TS interface
numeric学习之iota,accumulate
MVVM model
2022牛客多校第二场
Source code of YY music wechat applet imitating Netease cloud music
Very simple vsplayaudio online music player plug-in
WordPress removes the website publishing time
Function definition and call
【MUDUO】EventLoop事件循环
Node基础
Which securities firm is the best and safest for beginners to open an account
Why are there many snapshot tables in the BI system?
ES6 syntax (difference between let, const, VaR, deconstruction assignment, arrow function, residual parameters, extension method of array)
生成随机数random学习之uniform_int_distribution,uniform_real_distribution
Duplicate numbers in array
Solution of phpstudy service environment 80 port occupied by process system under Windows