日期:2014-05-20 浏览次数:20662 次
public class Singleton { private static Singleton instance = null; private Singleton(){} public static Singleton getInstance() { if (instance == null){ synchronized(Singleton.class){ if(instance == null) { instance = new Singleton(); } } } return instance; } }
public class Singleton { private static Singleton instance = null; private static final Object object = new Object(); private Singleton(){} public static Singleton getInstance() { if (instance == null){ synchronized(object){ if(instance == null) { instance = new Singleton(); } } } return instance; } }
public class Singleton { private static Singleton instance = new Singleton(); private Singleton() { // .... } public static Singleton getInstance() { return instance; } // 其它实作 }
public class Singleton { private static class SingletonHolder { static final Singleton instance = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.instance; } }