安卓中的单例模式的准确实现

2019-06-29  本文已影响0人  lovefo
public class A{
    private A() {}  //私有构造函数
    private volatile static Ainstance = null;  //单例对象
    //静态工厂方法
    public static A getInstance() {
          if (instance == null) {      //双重检测机制
         synchronized (A.class){  //同步锁
           if (instance == null) {     //双重检测机制
             instance = new A();
                }
             }
          }
          return instance;
      }
}
上一篇 下一篇

猜你喜欢

热点阅读