当前位置:网站首页>Puge -- singleton mode
Puge -- singleton mode
2022-06-28 06:32:00 【Dear-JC】
List of articles
The singleton pattern
Introduce
This pattern involves a single class , This class is responsible for creating its own objects , Also make sure that only a single object is created . This class provides a way to access its unique objects , You can directly access , There is no need to instantiate an object of this class .
- Main solution : A globally used class is frequently created and destroyed .
- When to use : When you want to control the number of instances , When saving system resources .
- Core logic : Constructor privatization .
Centralized implementation of singleton mode
Mode one : Slacker type ( Thread unsafe ) describe : This is the most basic way to achieve , The biggest problem with this implementation is that it doesn't support multithreading . Because there is no lock synchronized, So strictly speaking, it's not a singleton pattern .
This way, lazy loading Obviously , Thread safety is not required , Multithreading doesn't work .
public class Singleton {
private static Singleton instance; // Declare an instance of yourself
private Singleton (){
} // Write a privatization constructor
public static Singleton getInstance() {
// Write an instance get Method
if (instance == null) {
// Judge if there is no instance
instance = new Singleton(); // Create a new instance
}
return instance; // Return instance
}
}
Mode two : Slacker type ( Thread safety )
describe : This way has a good lazy loading, Can work well in multithreading , however , Efficiency is very low ,99% There is no need to synchronize . advantage : The first call initializes , Avoid memory waste . shortcoming : Must be locked. synchronized To guarantee the single case , But locking can affect efficiency .
getInstance() Performance is not critical for applications ( This method is not used frequently )
public class Singleton {
private static Singleton instance;
private Singleton (){
}
public static synchronized Singleton getInstance() {
// Add line lock
if (instance == null) {
// Judge if there is no instance
instance = new Singleton(); // Create a new instance
}
return instance; // Return instance
}
}
Mode three : Hungry Chinese style describe : This way is more commonly used , But it is easy to produce garbage objects . advantage : No locks , The efficiency of execution will improve . shortcoming : Class is initialized when it is loaded , Waste of memory .
It's based on classloader The mechanism avoids the synchronization problem of multithreading , however ,instance Instantiate at class load time , Although there are many reasons for class loading , In singleton mode, most of them call getInstance Method , But I'm not sure there are other ways ( Or other static methods ) Causes the class to load , This time initialization instance Obviously not lazy loading The effect of .
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton (){
}
public static Singleton getInstance() {
return instance;
}
}
Mode 4 : Double check lock / Double check lock (DCL, namely double-checked locking) describe : This method adopts double lock mechanism , It is safe and can maintain high performance in multithreading .
getInstance() Performance is critical to the application .
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 end !
边栏推荐
- YOLOv5增加小目标检测层
- Rn7302 three-phase electric quantity detection (based on STM32 single chip microcomputer)
- idea根据数据库表生成实体类
- 职场IT老鸟的几点小习惯
- Note that JPA uses a custom VO to receive jpql query results
- Scripting and programming languages
- fpm工具安装
- socke.io长连接实现推送、版本控制、实时活跃用户量统计
- Yolact++ Pytorch环境
- Yygh-7-user management
猜你喜欢

Yolov5 adds a small target detection layer

Drop down list processing in Web Automation

职场IT老鸟的几点小习惯

death_ satan/hyperf-validate

Socket. Io long Connection Push, version Control, Real - Time Active user volume Statistics

Triode driven brushless motor

Introduction to Qualcomm platform WiFi -- Wi Fi display software

浮动与定位

【Paper Reading-3D Detection】Fully Convolutional One-Stage 3D Object Detection on LiDAR Range Images

ROS rviz_ Satellite function package visualizes GNSS track and uses satellite map
随机推荐
No one can only use foreach to traverse arrays, right?
Parsing ng template with let total in NZ Pagination
Floating and positioning
Freeswitch sets the maximum call duration
AutoCAD C# 多段线自相交检测
Tryout title code
AutoCAD C polyline small acute angle detection
idea根据数据库表生成实体类
Unity packaging webgl uses IIS to solve the error
MySQL(二)——基本操作
Linked list (I) - remove linked list elements
普歌 -- 单例模式
Use the SQL SELECT count distinct query statement to count the total number of unique values of a field in the database
Some habits of it veterans in the workplace
2 startup, interrupt and system call
KMP string
eyebeam高级设置
Integer promotion and size side byte order
Differences between basic types and packaging classes
VM332 WAService.js:2 Error: _vm.changeTabs is not a function报错