单例模式

2018-12-06  本文已影响0人  响响月月
public class SingleObject {
    /**
     * 懒汉1
     * 线程不安全
     * 延时加载
     */
    static class SingletonLazy{
        private static SingletonLazy instance;
        private SingletonLazy(){}  //构造函数私有化
        public static SingletonLazy getInstance(){ //不支持多线程
            if(instance == null){       //getInstance如果空 new一个
                instance = new SingletonLazy();
            }
            return instance;
        }
    }
    /**
     * 懒汉2
     * 支持多线程,但效率低
     */
    static class SingletonLazySafe{
        private static SingletonLazySafe instance;
        private SingletonLazySafe(){}  //构造函数私有化
        public static synchronized SingletonLazySafe getInstance(){     //getInstance加锁
            if(instance == null){
                instance = new SingletonLazySafe();
            }
            return instance;
        }
    }
    /**
     * 饿汉
     */
    static class SingletonHungry{
        private static SingleObject instance = new SingleObject(); //装载时实例化,支持多线程,但垃圾多
        private SingletonHungry(){}  //构造函数私有化
        public static SingleObject getInstance(){
            return instance;
        }
    }
    /**
     * 双重检查!!!
     */
    static class SingletonDoubleCheck{
        private static volatile SingletonDoubleCheck instance;
        private SingletonDoubleCheck(){}
        public static SingletonDoubleCheck getInstance(){
            if(instance == null){
                synchronized (SingletonDoubleCheck.class){
                    if(instance == null){
                        instance = new SingletonDoubleCheck();
                    }
                }
            }
            return instance;
        }
    }
    /**
     *  静态域可以 实例域不行
     */
    static class SingletonStatic{
        private static class SingletonHolder {
            private static final SingletonStatic INSTANCE = new SingletonStatic();
        }
        private SingletonStatic(){}
        public static final SingletonStatic getInstance(){
            return SingletonHolder.INSTANCE;
        }
    }
    /**
     * 支持序列化
     */
    enum SingletonEnum {
        INSTANCE
    }
}

注:对于double-check 中变量为什么加volatile:
主要在于instance = new Singleton()这句,这并非是一个原子操作,事实上在 JVM 中这句话大概做了下面 3 件事情:

  1. 给 instance 分配内存
  2. 调用 Singleton 的构造函数来初始化成员变量
  3. 将instance对象指向分配的内存空间(执行完这步 instance 就为非 null 了)

但是在 JVM 的即时编译器中存在指令重排序的优化。也就是说上面的第二步和第三步的顺序是不能保证的,最终的执行顺序可能是 1-2-3 也可能是 1-3-2。如果是后者,则在 3 执行完毕、2 未执行之前,被线程二抢占了,这时 instance 已经是非 null 了(但却没有初始化),所以线程二会直接返回 instance,然后使用,然后顺理成章地报错。
有了volatile,限制了CPU对内存操作的重拍序,使其他线程在看到3之前2一定是执行过的。

上一篇 下一篇

猜你喜欢

热点阅读