1. 程式人生 > >JAVA#單例設計模式'執行緒札記

JAVA#單例設計模式'執行緒札記

class Singleton{
    private Singleton(){
    }
    private static Singleton sg=null;
    public static Singleton getSg(){

            if (sg == null) {
                synchronized (Singleton.class) {//使用所在類本身充當鎖
                    if (sg == null) {
                        sg = new Singleton();
                    }
                }
            }
        return sg;
    }
}