当前位置:网站首页>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
边栏推荐
- Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
- Rust web (I) -- self built TCP server
- Skywalking系列学习之告警通知源码分析
- 如何白嫖最新版BurpSuite Pro
- localStorage与sessionStorage
- Goatgui invites you to attend a machine learning seminar
- Arduino UNO +74hc164 water lamp example
- Inftnews | "traffic + experience" white lining e Digital Fashion Festival leads the new changes of digital fashion
- CS224W fall 1.2 Applications of Graph ML
- Debezium系列之:基于debezium offset拉取历史数据,确保数据没有丢失
猜你喜欢

Redis四大特殊数据类型的学习和理解

Boom 3D全新2022版音频增强应用程序App

Time module: acquisition and mutual transformation of timestamp, structured time and formatted time

Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community

CuteOne:一款OneDrive多网盘挂载程序/带会员/同步等功能

Concept of data asset management

Kubeadmin到底做了什么?

Lua函数之非全局函数

云开发寝适闹钟微信小程序源码

OpenTelemetry 在服务网格架构下的最佳实践
随机推荐
阿里云技术专家杨泽强:弹性计算云上可观测能力的构建
关于url编解码应该选用的函数
[hash table] question collection
[栈和队列简单题] LeetCode 232. 用栈实现队列,225. 用队列实现栈
Use of formdata
Plato Farm通过LaaS协议Elephant Swap,为社区用户带来全新体验
[Ryu] common problems and solutions in installing Ryu
商城小程序项目完整源码(微信小程序)
A test class understands beanutils.copyproperties
万字长文,带你搞懂 Kubernetes 网络模型
Complete source code of mall applet project (wechat applet)
typora详细教程
八皇后编程实现
Coco test dev test code
Kubernetes Dashboard 部署应用以及访问
基于GoLang实现API短信网关
coco test-dev 测试代码
ZJCTF_login
How to use devaxpress WPF to create the first MVVM application in winui?
CAS deployment and successful login jump address