Java设计模式——单例模式详解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32625839/article/details/81384854

三、单例模式的七种写法

3.1懒汉模式【线程不安全】,单例实例在第一次使用时进行创建。

public class SingletonExample1 {
    // 私有构造函数
    private SingletonExample1() {}
    // 单例对象
    private static SingletonExample1 instance = null;
    // 静态的工厂方法
    public static SingletonExample1 getInstance() {
        if (instance == null) {
            instance = new SingletonExample1();
        }
        return instance;
    }
}

3.2懒汉模式【线程安全】,【不推荐】,单例实例在第一次使用时进行创建。

public class SingletonExample3 {

    // 私有构造函数
    private SingletonExample3() {}

    // 单例对象
    private static SingletonExample3 instance = null;

    // 静态的工厂方法
    public static synchronized SingletonExample3 getInstance() {
        if (instance == null) {
            instance = new SingletonExample3();
        }
        return instance;
    }
}

3.3懒汉模式【线程不安全】,单例实例在第一次使用时进行创建。

public class SingletonExample4 {

    // 私有构造函数
    private SingletonExample4() {}

    // 代码看起来的执行顺序:
    // 1、memory = allocate() 分配对象的内存空间
    // 2、ctorInstance() 初始化对象
    // 3、instance = memory 设置instance指向刚分配的内存

    // JVM和cpu优化,发生了指令重排

    // 1、memory = allocate() 分配对象的内存空间
    // 3、instance = memory 设置instance指向刚分配的内存
    // 2、ctorInstance() 初始化对象

    // 假如一个线程A,一个线程B,A刚好在第3步分配内存空间,而还未初始化对象,B就判断不为null,直接return,因此不安全

    // 单例对象
    private static SingletonExample4 instance = null;

    // 静态的工厂方法,双重检测机制
    public static SingletonExample4 getInstance() {
        if (instance == null) {                         // B
            synchronized (SingletonExample4.class) {    // 同步锁
                if (instance == null) {
                    instance = new SingletonExample4(); // A - 3
                }
            }
        }
        return instance;
    }
}

3.4懒汉模式【线程安全】,单例实例在第一次使用时进行创建。

public class SingletonExample5 {

    // 私有构造函数
    private SingletonExample5() {}

    // 1、memory = allocate() 分配对象的内存空间
    // 2、ctorInstance() 初始化对象
    // 3、instance = memory 设置instance指向刚分配的内存

    // 单例对象 volatile + 双重检测机制 -> 禁止指令重排
    private volatile static SingletonExample5 instance = null;

    // 静态的工厂方法,双重检测机制 
    public static SingletonExample5 getInstance() {
        if (instance == null) {                         // B
            synchronized (SingletonExample5.class) {    // 同步锁
                if (instance == null) {
                    instance = new SingletonExample5(); // A - 3
                }
            }
        }
        return instance;
    }
}

3.5饿汉模式【线程安全】,单例实例在类装载时进行创建。

public class SingletonExample2 {

    // 私有构造函数
    private SingletonExample2() {}
    // 单例对象
    private static SingletonExample2 instance = new SingletonExample2();
    // 静态的工厂方法
    public static SingletonExample2 getInstance() {
        return instance;
    }
}

3.6饿汉模式【线程安全】,单例实例在类装载时进行创建。

public class SingletonExample6 {
    // 私有构造函数
    private SingletonExample6() {}
    // 单例对象
    private static SingletonExample6 instance = null;
    // 静态代码块
    static {
        instance = new SingletonExample6();
    }
    // 静态的工厂方法
    public static SingletonExample6 getInstance() {
        return instance;
    }
}

3.7枚举模式【线程安全】,【推荐使用】。

public class SingletonExample7 {

    // 私有构造函数
    private SingletonExample7() {}

    public static SingletonExample7 getInstance() {
        // 内部枚举类返回实例
        return Singleton.INSTANCE.getInstance();
    }
    // 内部枚举类
    private enum Singleton {
        INSTANCE;

        private SingletonExample7 singleton;

        // JVM保证这个方法绝对只调用一次
        Singleton() {
            singleton = new SingletonExample7();
        }

        public SingletonExample7 getInstance() {
            return singleton;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_32625839/article/details/81384854
今日推荐