一個簡單的單例模式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;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章