当前位置:网站首页>Single case mode (double check lock)
Single case mode (double check lock)
2022-07-27 03:10:00 【Sharp surge】
Use DCL Dual retrieval mode :
The code is as follows :
package SingLeton_pattern;
//import static com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type.String;
// Double check lock / Double check lock (DCL, namely double-checked locking)
public class Singleton {
private volatile static Singleton singleton;
private Singleton(){}
public static Singleton getSingleton()
{
if(singleton == null)
{
synchronized (Singleton.class)
{
if(singleton == null)
singleton = new Singleton();
}
}
return singleton;
}
}
The following code is not executed atomically in a multithreaded environment :
singleton = new Singleton();The normal low-level execution sequence will change into three steps :
(1) to Singleton Class singleton Allocate memory
(2) Call the instance singleton To initialize member variables
(3) take singleton Point to the allocated memory address The three steps above , No matter in A To which step is the thread currently executing , Yes B Threads may see A There can only be two states 1,2 All I see is null,3 See non null, That's fine .
But if there is an instruction rearrangement , namely (3) First executed. , that B The thread is in the first null When checking , It is possible to see that this example is not null, At this time, there will be problems if you use it directly .
So here we use :
volatile Instruction reordering is disabled , So the initialization sequence must be 1->2->3, Therefore, there is no case of getting uninitialized object references .It was used volatile Modifier member variable , Then after the variable assignment , There will be a memory barrier . That is to say, only after the implementation 1,2,3 After step operation , Read operation can see , Read operations are not reordered before write operations .
stay 《 In depth understanding of Java virtual machine 》 It is described in a book :
“ Observe the addition of volatile Keywords and did not add volatile Keyword generated assembly code found , Join in volatile When a keyword , One more lock Prefix instruction ”
lock The prefix instruction is actually a memory barrier ( It's also a memory fence ), The memory barrier will provide 3 Features :
advantage :
Keep it up DCL, Relatively simple
determine :volatile This keyword will bring some performance impact .
————————————————
Copyright notice : This paper is about CSDN Blogger 「 Big fish qss」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/qq_37857292/article/details/124566044
边栏推荐
- Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
- Coco test dev test code
- Debezium series: the binlog file cannot be recovered after the record is hung from the library server, and the task is switched to the main library to ensure that the data is not lost
- My crawler notes (VII) blog traffic +1 through Crawlers
- Concept of data asset management
- 基于GoLang实现API短信网关
- Compile and use protobuf Library in vs2019
- [动态规划中等题] LeetCode 198. 打家劫舍 740. 删除并获得点数
- [SQL simple question] leetcode 627. change gender
- MarqueeView实现滑动展示效果
猜你喜欢

毕业2年转行软件测试获得12K+,不考研月薪过万的梦想实现了

关于url编解码应该选用的函数

window对象的常见事件

Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩

次轮Okaleido Tiger即将登录Binance NFT,引发社区热议

Inftnews | "traffic + experience" white lining e Digital Fashion Festival leads the new changes of digital fashion

Social wechat applet of fanzhihu forum community

Skywalking系列学习之告警通知源码分析

Cloud development sleeping alarm clock wechat applet source code

身家破亿!86版「红孩儿」拒绝出道成学霸,已是中科院博士,名下52家公司
随机推荐
Go to export excel form
红宝书第四版的一个错误?
确定了,2022下半年软考报名8月开始
2649: segment calculation
【RYU】安装RYU常见问题及解决办法
Ten thousand words long text, take you to understand the kubernetes network model
Ansible series: do not collect host information gather_ facts: False
使用 WebSocket 实现一个网页版的聊天室(摸鱼更隐蔽)
Plato Farm全新玩法,套利ePLATO稳获超高收益
万字长文,带你搞懂 Kubernetes 网络模型
对象创建的流程分析
go实现导出excel表格
Rust web (I) -- self built TCP server
Use the most primitive method to manually implement the common 20 array methods
如何使用DevExpress WPF在WinUI中创建第一个MVVM应用程序?
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
cocos小游戏实战-05-NPC与角色攻击逻辑
机器学习【Matplotlib】
五、MFC视图窗口和文档
[SQL简单题] LeetCode 627. 变更性别