当前位置:网站首页>[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 .
边栏推荐
- Wrote a little webapi knowledge points from 0 to 1
- Overview of MES system equipment management (Part 2)
- Deep and shallow copies
- 新手开户选择哪个券商公司好呢?安全吗
- [wechat applet] page navigation
- POI special effects Market Research
- Idea sets get and set templates to solve the naming problem of boolean type fields
- Kotlin 常用知识点汇总
- JS get the current date and time
- Mongodb update operator (modifier)
猜你喜欢

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

PHP JSON variable array problem

意向不到的Dubug妙招

Release of v6.5.1/2/3 series of versions of Xingyun housekeeper: the ability of database OpenAPI continues to be strengthened

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

Wamp MySQL empty password

Data broker understanding

Dynamic memory management

Npm+ module loading mechanism

Several commonly used traversal methods
随机推荐
Es5 new method
图的遍历-DFS,BFS(代码详解)
Node Foundation
Promise asynchronous callback function
Call Gaode map -- address is converted into longitude and latitude
R language drawing parameters (R language plot drawing)
类和对象(2)(6个默认成员函数)
Very simple vsplayaudio online music player plug-in
Dynamic memory management
CSV generated by PHP cannot completely display the number with leading 0
Query commodity cases (operate data with array addition method) / key points
Which securities firm is the best and safest for beginners to open an account
About the foundation of fetch
PHP wechat scan code, follow official account and authorize login source code
电商RPA,大促轻松上阵的法宝
Rendering, filtering (filtering) and sorting of lists
PyTorch的数据输入格式要求及转换
[QNX Hypervisor 2.2用户手册]9.7 generate
[QNX Hypervisor 2.2用户手册]9.8 load
Unity uses macros