package www.demo.Designpattern.单件模式; public class Singleton { private static Singleton uniqueSingleton; private Singleton(){ } private static synchronized Singleton getInstance(){ if (uniqueSingleton==null) { uniqueSingleton = new Singleton(); } return uniqueSingleton; } }
package www.demo.Designpattern.单件模式; public class Singleton { private volatile static Singleton uniqueSingleton; private Singleton(){ } private static Singleton getInstance(){ if (uniqueSingleton==null) { synchronized (Singleton.class) { if (uniqueSingleton==null) { uniqueSingleton = new Singleton(); } } } return uniqueSingleton; } }