多线程下单例模式

public class Singleton {

    private static volatile  Singleton singleton = null;

    private Singleton(){}

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

 

你可能感兴趣的:(多线程下单例模式)