singleton单例模式的几种创建方式

单例模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。

即:①单例只能有一个实例②单例类必须自己创建自己的唯一实例③单例类必须给其他对象提供这一实例

1.懒汉模式,线程不安全

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

2.懒汉模式,线程安全

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

3.饿汉模式,线程安全

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

4.双检锁/双重校验锁(DCL,即double-checked locking)

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

5.登记式/静态内部类

public class Singleton {
    private static class SingletonHolder{
        private static final Singleton instance=new Singleton();
    }
    private Singleton(){}
    public static final Singleton getInstance(){
        return SingletonHolder.instance;
    }
}

6.枚举

public enum  Singleton {
    INSTANCE;
    public void whateverMethod(){
        
    }
}

总结:通常的情况下是不建议使用第一种和第二种懒汉模式的,推荐使用第三种饿汉模式。在明确要使用懒汉模式的时候才会使用第五种模式。。如果涉及到反序列化创建对象时,可以使用枚举。如果还有其他其他的特殊需求,可以考虑使用双检锁模式。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在多线程创建单例模式通常有以下几种方式: 1. 懒汉式(线程不安全):在多线程环境中,如果多个线程同时调用该方法,可能会导致创建多个实例的问题,从而破坏单例模式的设计。因此,该方式不适用于多线程环境。 ```python class Singleton: instance = None @staticmethod def get_instance(): if Singleton.instance is None: Singleton.instance = Singleton() return Singleton.instance ``` 2. 懒汉式(线程安全):在多线程环境中,使用锁来确保只有一个线程可以访问该方法,从而保证单例模式的设计。 ```python import threading class Singleton: instance = None lock = threading.Lock() @staticmethod def get_instance(): with Singleton.lock: if Singleton.instance is None: Singleton.instance = Singleton() return Singleton.instance ``` 3. 饿汉式(线程安全):在多线程环境中,由于该方式在模块被加载时就创建了一个实例,因此可以保证只有一个实例被创建并被多个线程共享。但是,如果该实例很大,或者需要耗费很多资源来创建,可能会影响程序的性能。 ```python class Singleton: instance = Singleton() @staticmethod def get_instance(): return Singleton.instance ``` 4. 双重检查锁定(线程安全):该方式结合了懒汉式和饿汉式的优点,既可以在需要时创建实例,又可以保证只有一个实例被创建并被多个线程共享。但是,由于 Python 的 GIL(全局解释器锁)机制,可能会影响程序的性能。 ```python import threading class Singleton: instance = None lock = threading.Lock() @staticmethod def get_instance(): if Singleton.instance is None: with Singleton.lock: if Singleton.instance is None: Singleton.instance = Singleton() return Singleton.instance ``` 在多线程环境中,建议使用双重检查锁定方式创建单例模式,因为它既可以确保只有一个实例被创建并被多个线程共享,又可以在需要时才创建实例,从而避免了饿汉方式可能会影响程序性能的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值