当前位置:网站首页>普歌 -- 单例模式
普歌 -- 单例模式
2022-06-28 06:29:00 【Dear-JC】
单例模式
介绍
这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。
- 主要解决:一个全局使用的类频繁地创建与销毁。
- 何时使用:当您想控制实例数目,节省系统资源的时候。
- 核心逻辑:构造函数私有化。
单例模式的集中实现方式
方式一:懒汉式(线程不安全)描述:这种方式是最基本的实现方式,这种实现最大的问题就是不支持多线程。因为没有加锁 synchronized,所以严格意义上它并不算单例模式。
这种方式 lazy loading 很明显,不要求线程安全,在多线程不能正常工作。
public class Singleton {
private static Singleton instance; // 声明一个自己的实例
private Singleton (){
} // 写一个私有化构造方法
public static Singleton getInstance() {
// 写一个获取实例的get方法
if (instance == null) {
// 判断如果没有实例
instance = new Singleton(); // 新建一个实例
}
return instance; // 返回实例
}
}
方式二:懒汉式(线程安全)
描述:这种方式具备很好的 lazy loading,能够在多线程中很好的工作,但是,效率很低,99% 情况下不需要同步。优点:第一次调用才初始化,避免内存浪费。缺点:必须加锁 synchronized 才能保证单例,但加锁会影响效率。
getInstance() 的性能对应用程序不是很关键(该方法使用不太频繁)
public class Singleton {
private static Singleton instance;
private Singleton (){
}
public static synchronized Singleton getInstance() {
// 加线程锁
if (instance == null) {
// 判断如果没有实例
instance = new Singleton(); // 新建一个实例
}
return instance; // 返回实例
}
}
方式三:饿汉式描述:这种方式比较常用,但容易产生垃圾对象。优点:没有加锁,执行效率会提高。缺点:类加载时就初始化,浪费内存。
它基于 classloader 机制避免了多线程的同步问题,不过,instance 在类装载时就实例化,虽然导致类装载的原因有很多种,在单例模式中大多数都是调用 getInstance 方法, 但是也不能确定有其他的方式(或者其他的静态方法)导致类装载,这时候初始化 instance 显然没有达到 lazy loading 的效果。
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton (){
}
public static Singleton getInstance() {
return instance;
}
}
方式四:双检锁/双重校验锁(DCL,即 double-checked locking)描述:这种方式采用双锁机制,安全且在多线程情况下能保持高性能。
getInstance() 的性能对应用程序很关键。
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;
}
}
完结!
边栏推荐
- 报错--解决core-js/modules/es.error.cause.js报错
- 慢内容广告:品牌增长的长线主义
- 微信小程序编译页面空白bug的原因
- Floating and positioning
- No one can only use foreach to traverse arrays, right?
- Eyebeam advanced settings
- KMP string
- Use the SQL SELECT count distinct query statement to count the total number of unique values of a field in the database
- 移动广告发展动向:撬动存量,精细营销
- Install redis on windows and permanently change the password, and integrate redis with the SSM framework
猜你喜欢

CAD二次开发+NetTopologySuite+PGIS 引用多版本DLL问题

MySQL (I) - Installation

FPGA - 7系列 FPGA SelectIO -07- 高级逻辑资源之ISERDESE2

Integer promotion and size side byte order

Students who do not understand the code can also send their own token. The current universal dividend model can be divided into BSC and any generation B

Slow content advertising: the long-term principle of brand growth

Difficulty calculation of Ethereum classic

微信小程序编译页面空白bug的原因

The custom cube UI pop-up dialog supports multiple and multiple types of input boxes

Linked list (I) - remove linked list elements
随机推荐
lombok @EqualsAndHashCode 注解如何让对象.equals()方法只比较部分属性
Freeswitch sets the maximum call duration
Install redis on windows and permanently change the password, and integrate redis with the SSM framework
No one can only use foreach to traverse arrays, right?
Deleting MySQL under Linux
death_satan/hyperf-validate
Unity packaging webgl uses IIS to solve the error
自定义 cube-ui 弹出框dialog支持多个且多种类型的input框
CAD二次开发+NetTopologySuite+PGIS 引用多版本DLL问题
MySQL (II) - basic operation
Rn7302 three-phase electric quantity detection (based on STM32 single chip microcomputer)
使用SQL select count distinct查询语句统计数据库中某个字段的唯一值总数量
使用SSM框架,配置多个数据库连接
Linux MySQL implements root user login without password
语音增强-频谱映射
Promotion intégrale et ordre des octets de fin de taille
How the third-party libraries in cocoapod reference local header files
Online facing such an online world, the only limitation is our imagination
sql及list去重操作
Yolact++ Pytorch环境