• 【线程安全】—— 单例类双重检查加锁(double-checked locking)


    1. 三个版本单例类的实现

    • 版本1:经典版

      public class Singleton {
          public static Singleton getInstance() {
              if (instance == null) {
                  // 进入 if 之后(也就是 instance 为空时),此时如果另外一个进程刚好执行到 instance = new Singleton(); 则破坏了单例类的唯一性;      
                  instance = new Singleton();
              }
              return instance;
          }
          private Singleton() {}
          private static Singleton instance;
      }

      缺陷:非线程安全,当一个线程进入 getInstance() 方法的 if 判断,而未创建赋值新的对象实例之前,另外一个线程进入 if 判断,将产生两个“单例”对象;

    • 版本2:同步版

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

      synchronized 关键字保证了,每一个方法进入该方法之前,都要等到其他方线程离开该方法。

      该方法的缺陷在于,低效。事实上,只在第一次实例化对象时,需要加锁,或者叫同步(版本2方法,每次都要进行加锁验证)。后续获取实例时,多个线程是可以进入同时进入该方法的,因为 if 判断必然通不过,直接返回单例实例。

    • 版本3:双重检查加锁:

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

    2. 使用 lock 机制(C++ 版)

    class Singleton {
        private:
            volatile Singleton* pInst = 0;
        public:
            static Singleton* GetInstance() {
                if (pInst == 0) {
                    lock();
                    if (pInst == 0) {
                        pInst = new Singleton();
                    }
                    unlock();
                }
                return pInst;
            }
    }

    同步机制(synchronized)等价于锁机制;

  • 相关阅读:
    C_数据结构_栈
    C_数据结构_链表
    C_数据结构_数组的修改和删除
    C_数据结构_数组
    Python_闭包_27
    Python_函数的镶嵌和作用域链_26
    P1428 小鱼比可爱
    P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
    P2946 [USACO09MAR]牛飞盘队Cow Frisbee Team
    codevs 2173 忠诚
  • 原文地址:https://www.cnblogs.com/mtcnn/p/9421240.html
Copyright © 2020-2023  润新知