多线程下单例模式:
public class LazySingletonTest { public static void main(String[] args) { LazySingleton instance1 = LazySingleton.getInstance(); LazySingleton instance2 = LazySingleton.getInstance(); System.out.println(instance1 == instance2); } } class LazySingleton { private static LazySingleton lazySingleton; public LazySingleton() { } public static LazySingleton getInstance() { if (lazySingleton == null) { synchronized (LazySingleton.class) { if (lazySingleton == null) { return lazySingleton = new LazySingleton(); } } } return lazySingleton; } }