单例模式(懒汉)(一)
/** * 单例模式(懒汉)线程不安全 */ public class Single { private static Single single=null; private Single(){ } public static Single getInstance(){ if (single == null){ single=new Single(); } return single; }
测试是否为单例
public static void main(String[] args) { Single single1 = Single.getInstance(); Single single2 = Single.getInstance(); System.out.println(single1 == single2); }