当前位置:网站首页>Talk about singleton mode!
Talk about singleton mode!
2022-06-25 23:51:00 【Program diary】
The singleton pattern
Definition
Ensure that a class has only one instance , And provide the global access point of the instance , This class can only be new once , And I use it every time new This .
Class structure
A private constructor , A private static instance variable , A public static function , Used to get instances .
Private constructor : Can not let others create
Common static functions : The global access point for this instance , Returns the unique private static instance variable
Concrete realization
Slacker type - Thread unsafe
The lazy type creates the return object only when it is used , It will not be created when it is not needed , But there are also thread unsafe problems
Thread unsafe : Multiple threads access the acquisition object for the first time at the same time , Both are judged to be empty , And then new The object , Result in multiple instantiations of the object
public class Singleton {
private static Singleton uniqueInstance;
private Singleton(){
}
public static Singleton getUniqueInstance(){
if (uniqueInstance == null){
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
}
Slacker type - Thread safety
How to solve the lazy thread insecurity problem ? Make it thread safe , You only need to lock the instance when you get it , In this way, only one thread can enter the method of obtaining instances at the same time , Thread insecurity can be avoided !
shortcoming : Because it is locked , Only one thread can access this method at a time , Performance issues , Not recommended .
public static synchronized Singleton getUniqueInstance(){
if (uniqueInstance == null){
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
Hungry Chinese style - Thread safety
It is also to solve the lazy thread insecurity problem , Just instantiate it directly , No longer use deferred instantiation , But no deferred instantiation saves resources
public class Singleton {
private static Singleton uniqueInstance = new Singleton();
private Singleton(){
}
public static synchronized Singleton getUniqueInstance(){
return uniqueInstance;
}
}
Double check lock - Thread safety
Just lock the instance , Do not lock the method
public class Singleton {
private volatile static Singleton uniqueInstance;
private Singleton(){
}
public static Singleton getUniqueInstance(){
if (uniqueInstance == null){
synchronized (Singleton.class){
if (uniqueInstance == null){
uniqueInstance = new Singleton();
}
}
}
return uniqueInstance;
}
}
First explain why you use volatile keyword
prohibit jvm The command rearrangement of , There are three steps to instantiate an object
- Allocate memory space
- Initialize object
- Point the object to the allocated memory address
This order is in jvm The executor in may be out of order , For example 132 In this way , When a thread finishes executing 13 after , Another thread just entered if Judgment is not empty , An uninitialized object is returned directly , So in order to solve this problem, we use volatile Keyword can prevent instruction rearrangement
Explain why you use two if
When two threads enter the first one at the same time if, Both are judged not to be empty , Then lock it , A thread performs the operation of instantiating objects , After instantiation , Another thread also locks in , But if there is no second if Words , The second process will also be instantiated , After that, there will be no second instantiation .
Static inner class implementation - Thread safety
Is to use a static inner class , Class to write a static constant ,Singleton The inner class is not called when the class is loaded , Only when you need to get an instance, you can call the internal class to get it .
Implements deferred instantiation + Thread safety
public class Singleton {
private Singleton(){
}
private static class SingletonHolder{
private static final Singleton INSTANCE = new Singleton();
}
public static Singleton getUniqueInstance(){
return SingletonHolder.INSTANCE;
}
}
边栏推荐
- STL教程5-STL基本概念及String和vector使用
- Anaconda一文入门笔记
- mysql版本升级+数据迁移
- 给定参数n,从1到n会有n个整数1,2,3,...,n,这n个数组共有n!种排列,按照大小顺序升序排列出所有列的情况,并一一标记,给定n和k,返回第k个值
- 动态验证码
- MySQL InnoDB锁知识点
- 在win10下使用visual studio2015链接mysql数据库
- 解析产品开发失败的5个根本原因
- The InputStream stream has been closed, but the file or folder cannot be deleted, indicating that it is occupied by the JVM
- Let's talk about string today
猜你喜欢
随机推荐
产品经理如何把控产品开发的进度
史上最简单的录屏转gif小工具LICEcap,要求不高可以试试
Alipay payment interface sandbox environment test and integration into an SSM e-commerce project
UE4 learning records create a role and control its movement
Backup restore of xtrabackup
第六章 习题(678)【微机原理】【习题】
剑指 Offer 48. 最长不含重复字符的子字符串
CXF
谷歌浏览器(Chrome)最新v80版本下载
Apache Doris1.0版本集群搭建、负载均衡与参数调优
OpenResty篇01-入门简介和安装配置
虚析构和纯虚析构及C ++实现
SSL/TLS、对称加密和非对称加密和TLSv1.3
phoenix索引
MySQL version upgrade + data migration
推荐系统设计
On the quantity control mechanism of swoole collaboration creation in production environment
谈一谈生产环境中swoole协程创建数量控制机制
Gradle的环境安装与配置
213.打家劫舍 II