一个简单的单例模式Demo

/**
 * @author :nx014924
 * @date :Created in 5/30/2021 1:09 PM
 * @description:
 * @modified By:
 * @version:
 */
public class Singleton {
    private static Singleton singleton;

    //双检锁实现懒汉式单例模式
    public static Singleton getSingleton(){
        if (singleton == null){
            synchronized (Singleton.class){
                if (singleton == null)
                    singleton = new Singleton();
            }
        }
        return singleton;
    }
}

 

posted @ 2021-05-30 13:58  码出地球  阅读(44)  评论(0编辑  收藏  举报