HeadFirst(五)Singleton 单件设计模式
package lazy;public class Singleton {//用到的时候才实例化private static Singleton uniqueInstance = null;private Singleton() {}/** * 使用双重检查加锁,确保多线程环境不出问题 */public static Singleton getInstance() {if(uniqueInstance==null) {synchronized(Singleton.class) {if(uniqueInstance==null) {uniqueInstance = new Singleton();}}}return uniqueInstance;}}
?