当前位置:网站首页>[concurrent programming 053] Why use volatile variables for double check lock variables
[concurrent programming 053] Why use volatile variables for double check lock variables
2022-04-22 12:17:00 【Tanyue sword finger factory】
Double check why lock variables are used volatile Variable

Look at the notes in the figure , Assuming that thread A perform getInstance Method
Up to the third day 4 Step : instance by null, entering if Judge ;
Up to the third day 5 Step : obtain synchronized lock , success , Go to sync code block ;
Up to the third day 6 Step : Continue to judge instance, by null entering if Judge ;
Up to the third day 7 Step : instance = new Instance() . It seems to be a sentence of code , It's actually three sentences of code .

Up to the third day 7.1 Step : Allocate memory space for objects .
Execute to 7.3 Step : about 7.2 Step sum 7.3 Step , There is no dependency between the two , Set up instance Points to the memory address just allocated .

Just in the thread A After execution 7.3 After step , Not yet implemented 7.2 When initializing objects , Threads B coming .
Look at the threads B:
Execution section 4 Step : instance Not for null, Do not enter if Judge ; ( Why at this time instance Not for null? because == The judgment of the , The judgment is internal Storage address , Threads A Although the object is not initialized , But it has been set instance Point to the memory address , So mistakenly think that at this time instance No by null)
Execution section 10 Step : return instance; ( Because the initialization object has not been executed yet , here instance The corresponding instance object is null) .
Something is wrong. .
So join volatile, prohibit 7.2 and 7.3 The reordering of , Problem solved .
版权声明
本文为[Tanyue sword finger factory]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221215581125.html
边栏推荐
- 2019年华为鸿蒙加入手机系统阵营,如何看待鸿蒙这三年的发展?
- The higher the refresh rate, the better?
- Some problems in the use of zuul
- JS 【詳解】作用域
- When the new version of redis is released, do you still think redis is a single thread?
- Redis新版本发布,你还认为Redis是单线程?
- Synchronized lock and its expansion
- [in depth understanding of tcallusdb technology] scan data interface description - [list table]
- Electrician Lecture 1
- 【深入理解TcaplusDB技术】读取列表指定位置数据接口说明——[List表]
猜你喜欢

"Open source summer" activity is hot. In the registration, rich bonuses are waiting for you to get!
![[in depth understanding of tcallusdb technology] description of data interface for batch deletion of specified location in list - [list table]](/img/ed/cccd5dee09d2f0a3e6c788bd265b36.png)
[in depth understanding of tcallusdb technology] description of data interface for batch deletion of specified location in list - [list table]

.NET宝藏API之:OutputFormatter,格式化输出对象
![【深入理解TcaplusDB技术】读取列表指定位置数据接口说明——[List表]](/img/0d/1f70f1dba4e746d81eccb7ad3cea5d.png)
【深入理解TcaplusDB技术】读取列表指定位置数据接口说明——[List表]
![[in depth understanding of tcallusdb technology] scan data interface description - [list table]](/img/ed/cccd5dee09d2f0a3e6c788bd265b36.png)
[in depth understanding of tcallusdb technology] scan data interface description - [list table]

A note that allows you to capture the offer of a large factory. Come and get it quickly

. net treasure API: outputformatter, format output object

Électricien deuxième Conférence

C language DLL Dynamic Link Library

【深入理解TcaplusDB技术】示例代码——异步调用接口
随机推荐
2019年华为鸿蒙加入手机系统阵营,如何看待鸿蒙这三年的发展?
Interpretation of tamigou project | 49.5% equity transfer of Beijing Hualong pawn Co., Ltd
What happens when you run the NPM install command?
电路实验——实验四 戴维南定理与诺顿定理
日撸代码300行学习笔记 Day 47
案例4-1.4:堆中的路径 (小顶堆的模拟建立和模拟路径)
Base64 encryption, decryption and JSON processing
"Open source summer" activity is hot. In the registration, rich bonuses are waiting for you to get!
[deeply understand tcallusdb technology] delete all data interface descriptions in the list - [list table]
【并发编程049】说说重排序的分类?
Best buy website EDI test process
1086 tree traversals again (25 points)
【并发编程052】说说双重检查锁以及其优点?
有研究显示,现在年轻人越来越不愿意换手机了。下一代智能手机在硬件上出现哪些更新,才会让你有换机的冲动?
一种自动切换过流保护模块的热泵装置保护电路介绍(ACS758/CH704应用案例)
Esp32-cam usage history
MySQL学习第四弹——多表查询分类以及案例练习源码详解
What does 0ul or 1ul in STM32 mean?
【深入理解TcaplusDB技术】删除列表所有数据接口说明——[List表]
[in depth understanding of tcallusdb technology] description of data interface for batch deletion of specified location in list - [list table]