当前位置:网站首页>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;
}
}
边栏推荐
- 聊聊swoole或者php cli 进程如何热重启
- 为什么Integer的比较最好使用equals
- DPVS-FullNAT模式管理篇
- iomanip头文件在实战中的作用
- php socket通信中stream_select方法的理解
- Apache doris1.0 cluster setup, load balancing and parameter tuning
- Alipay payment interface sandbox environment test and integration into an SSM e-commerce project
- Gradle的环境安装与配置
- php进程间传递文件描述符
- 在win10下使用visual studio2015链接mysql数据库
猜你喜欢
随机推荐
51 single chip microcomputer, some registers, some knowledge points
php中使用google protobuf协议环境配置
4个要点,助力产品管好项目
Megacli常用命令整理
OpenResty篇01-入门简介和安装配置
Uniapp -- the use of document collation and push of unipush
util. Collection and encapsulation of JS tool functions
Line height for small use
格式化编号,不够位数的补0,例如1:0001,25:0025
Anaconda一文入门笔记
Problems encountered in Doris operation and maintenance
平衡二叉树AVL
对伪类的理解
unsigned与signed之大白话
Px4 multi computer simulation (gazebo)
The simplest screen recording to GIF gadget in history, licecap, can be tried if the requirements are not high
IDEA常用快捷键
Apache Doris1.0版本集群搭建、负载均衡与参数调优
Tensorflow中CSV文件数据读取
.user.ini文件导致的php网站安装问题








