正确的“懒汉式”单例写法

public class Singleton {
    private volatile static Singleton INSTANCE; //声明成 volatile
    private Singleton (){}

    public static Singleton getSingleton() {
        if (INSTANCE == null) {                         
            synchronized (Singleton.class) {
                if (INSTANCE == null) {       
                    INSTANCE = new Singleton();
                }
            }
        }
        return INSTANCE;
    }

}

你可能感兴趣的:(Android开发经验)