用 Java 写一个线程安全的单例模式(Singleton)?
public class Singleton{ private static Singleton singleton; private Singleton (){ } public static Singleton getInstance(){ if(singleton == null){ singleton = new Singleton(); } return singleton; } }
public class Singleton{ private static Singleton singleton; private Singleton(){} public static synchronized Singleton getInstance(){ if(singleton == null){ singleton = new Singleton(); } return singleton; } }
public class Singleton{ private static Singleton singleton; private Singleton(){} public static Singleton getInstance(){ if(singleton == null){ synchronized(Singleton.class){ if(singleton == null ) singleton = new Singleton(); } } return singleton; } }