public class Singleton { private Singleton() { System.out.println(Thread.currentThread().getName() + " 初始化"); } // 使用volatile的可见性 private volatile static Singleton instance = null; public static Singleton getInstance() { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { instance = new Singleton(); } } } return instance; } }
并发编程下单例模式
最新推荐文章于 2024-06-17 20:59:29 发布