//懒汉单例模式:单例的延迟加载方式 class Single2{ private Single2(){} private static Single2 s = null; public static Single2 getInstance(){ if(s==null){ s = new Single2(); } return s; } }
解决懒汉式线程安全问题后的写法:
class Single2{private Single2(){}private static Single2 s;public static Single2 getInstance(){if (s==null) {synchronized (Single2.class) {if (s==null) {s = new Single2();}}}return s;}}