19. 正确看待单例模式

2018-12-11  本文已影响0人  TheMrBigHead

单线程版单例模式实现

/**
* 单线程下的单例模式实现
*/
public class SingleThreadSingleton {

    // 保存该类的唯一实例
    private static SingleThreadSingleton instance = null;

    /**
    * 私有构造方法
    */
    private SingleThreadSingleton() {

    }

    public static SingleThreadSingleton getInstance() {
        if(null == instance) {  // 操作1
            instance = new SingleThreadSingleton(); // 操作2
        }

        return instance;
    }

}

这个代码在多线程情况下,getInstance()中的if操作不是一个原子操作,可能会导致多个实例的创建。

简单加锁版单例模式实现

public class SimpleMultipleThreadSingleton {

    // 保存该类的唯一实例
    private static SimpleMultipleThreadSingleton instance = null;

    /**
    * 私有构造方法
    */
    private SimpleMultipleThreadSingleton() {

    }

    public static SimpleMultipleThreadSingleton getInstance() {

        synchronized(SimpleMultipleThreadSingleton.class) { // 加锁
            if(null == instance) {
                instance = new SimpleMultipleThreadSingleton();
            }
        }

        return instance;
    }

}

虽然是线程安全的,但是每次在调用getInstance()方法时都会去申请锁和释放锁,产生了资源开销。

基于双重检查锁定的单例模式 (不再提倡使用)

错误实现
public class IncrrentDCLSingleton {

    // 保存该类的唯一实例
    private static IncrrentDCLSingleton instance = null;

    /**
    * 私有构造方法
    */
    private IncrrentDCLSingleton() {

    }

    public static IncrrentDCLSingleton getInstance() {

        if(null == instance) { // 操作1
            synchronized(IncrrentDCLSingleton.class) { 
                if(null == instance) { // 操作2
                    instance = new IncrrentDCLSingleton(); // 操作3
                }
            }
        }

        return instance;
    }

}

重排序可能会导致错误

错误原因

解决方法:给instance变量添加volatile修饰符即可

解决方法
正确实现
public class DCLSingleton {

    // 保存该类的唯一实例
    private static volatile DCLSingleton instance = null;

    /**
    * 私有构造方法
    */
    private DCLSingleton() {

    }

    public static DCLSingleton getInstance() {

        if(null == instance) { // 操作1
            synchronized(DCLSingleton.class) { 
                if(null == instance) { // 操作2
                    instance = new DCLSingleton(); // 操作3
                }
            }
        }

        return instance;
    }

}

基于静态内部类的单例模式实现

public class StaticHolderSingleton {

    // 保存该类的唯一实例
    private static volatile StaticHolderSingleton instance = null;

    /**
    * 私有构造方法
    */
    private StaticHolderSingleton() {

    }

    private static class StaticHolderSingletonHolder {
        // 保存StaticHolderSingleton的唯一实例
        final static StaticHolderSingleton INSTANCE = new StaticHolderSingleton();
    }

    public static StaticHolderSingleton getInstance() {
        return StaticHolderSingletonHolder.INSTANCE;
    }

}
原理

基于枚举的单例模式实现

public enum Singleton {
    INSTANCE;

    Singleton() {
        
    }
}
原理
上一篇 下一篇

猜你喜欢

热点阅读