当前位置:网站首页>[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 .
边栏推荐
- chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted
- What is a physical firewall? What's the effect?
- [QNX hypervisor 2.2 user manual]9.8 load
- Computed and watch listening properties
- Node Foundation
- Release of v6.5.1/2/3 series of versions of Xingyun housekeeper: the ability of database OpenAPI continues to be strengthened
- Firewall command simple operation
- Bind class style and bind style style
- 762. Prime number calculation setting in binary representation
- Query commodity cases (operate data with array addition method) / key points
猜你喜欢

类和对象(2)(6个默认成员函数)

Qt风格(QSS)应用之QProgressBar

Several commonly used traversal methods

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

电商RPA,大促轻松上阵的法宝

utility实用组件学习之swap,move,forward,exchange

【JUC】并发需要了解的关键字volatile

Classes and objects (3)

This point inside the function / change this point inside the function

Take away applet with main version of traffic / repair to add main access function of traffic
随机推荐
@Import
Why are there many snapshot tables in the BI system?
CSV generated by PHP cannot completely display the number with leading 0
762. 二进制表示中质数个计算置位
【微信小程序】页面导航
WordPress controls the minimum and maximum number of words of article comments
[QNX Hypervisor 2.2用户手册]9.8 load
Serialize common default values and column parameters
XxE & XML external entity injection utilization and bypass
WebMvcConfigurationSupport
Npm+ module loading mechanism
Serialize operator
Ffmpeg first learning (only for coding)
推荐系统——An Embedding Learning Framework for Numerical Features in CTR Prediction
PHP wechat scan code, follow official account and authorize login source code
行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化
学习探索-波浪
日期类的实现
策略模式_
Simulate and implement common interfaces of string class