Java单例模式

单例模式

定义:保证一个类仅有一个实例,并提供一个访问它的全局访问点

单例模式总共有5种实现

  • 饿汉模式
  • 懒汉模式
  • DCL(双重检查模式)----很重要
  • 静态内部类单例模式
  • 枚举类单例

(1)饿汉模式

public class Singleton{
    private static Singleton instance = new Singleton();
    private Singleton(){
        
    }
    
    public static Singleton getInstance(){
        return instance;
    }
}
  • 在类加载时就完成初始化,所以类加载较慢,但获取对象的速度快
  • 在类加载时就完成初始化,避免了多线程的问题
  • 在类加载时就完成实例化,没有达到懒加载效果
  • 如果从始至终都没有使用过这个实例,就会造成内存的浪费

(2)懒汉模式

线程不安全:

public class Singleton{
    private static Singleton instance;
    private Singleton(){
        
    }
    
    public static Singleton getInstance(){
        if(instance == null){
            instance = new Singleton();
        }
        return instance;
    }
}

线程安全:

public class Singleton{
    private static Singleton instance;
    private Singleton(){
        
    }
    
    public static synchronized Singleton getInstance(){
        if(instance == null){
            instance = new Singleton();
        }
        return instance;
    }
}
  • 线程安全
  • 但每次调用getInstance方法时都需要进行同步(造成不必要的同步开销,而且很多时候我们是用不到同步的)
  • 不建议使用

(3)双重检查模式(DCL)

public class Singleton{
    private static volatile Singleton instance;
    private Singleton(){
        
    }
    
    public static Singleton getInstance() {
        if(instance == null) {
            synchronized(Singleton.class) {
                if(instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}
(4)静态内部类单例模式
public class Singleton{
    private Singleton(){
        
    }
    public static Singleton getInstance getInstance() {
        return SingletonHolder.sInstance;
    }
    
    private static class SingletonHolder {
        private staic final Singleton sInstance = new Singleton();
    }
}

第一次加载Singleton类时并不会初始化sInstance,只有第一次调用getInstance方法时虚拟机加载 SingletonHolder 并初始化 sInstance。这样不仅能确保线程安全,也能保证 Singleton 类的唯一性。

所以,推荐使用静态内部类单例模式。

(5)枚举单例
public enum Singleton {
    INSTANCE;
    public void doSomeThing() {
        
    }
}

默认枚举实例的创建是线程安全的,并且在任何情况下都是单例。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值