package com.lfy.SingletonMode;

/**
 * 2、单例模式之懒汉模式(线程不安全)
 * @author lfy
 * @since 2018/06/03
 * @see   这方式是线程不安全的,测试看TestWithMultiThreading.java中的测试代码
 * */
public class SingletonMode2 {
    
    //构造器私有化
    private SingletonMode2(){
         
    }
    
    //在类的内部自己创建实例
    private static SingletonMode2 singleton = null;
 
    //提供get 方法以供外界获取单例
    public static SingletonMode2 getInstance(){
        //在这里就出现问题了,假设A线程比B线程稍早到达
        if(singleton == null){
            singleton = new SingletonMode2();
        }
        return singleton;
    }
}

 

相关文章:

  • 2022-12-23
  • 2021-05-27
  • 2022-12-23
  • 2022-12-23
  • 2021-06-30
  • 2021-10-25
  • 2021-09-25
  • 2022-01-08
猜你喜欢
  • 2021-08-12
  • 2021-08-06
  • 2021-10-17
  • 2021-05-18
  • 2021-07-17
  • 2021-06-05
  • 2022-12-23
相关资源
相似解决方案