设计模式
CodeBoyWLC
技术+原理爱好者
展开
-
【设计模式】单例模式(懒汉式)==>使线程安全:Double-Check方式
加同步块与volatile即可 public class MyBenchmark { public static void main(String[] args) { new Thread(() -> { Singleton instance1 = Singleton.getInstance(); }, "t1").start(); new Thread(() -> { Singleton instance2 = Singleton.getInstance();原创 2021-05-03 09:55:16 · 155 阅读 · 0 评论 -
【设计模式】单例模式(懒汉式)==>使线程安全
创建对象实例的static方法前加synchronized即可 public class SingletonTest03 { public static void main(String[] args) { new Thread(()-> { Singleton instance1 = Singleton.getInstance(); },"t1").start(); new Thread(()-> { Singleton instance2 = Singleto原创 2021-05-03 09:34:21 · 44 阅读 · 0 评论 -
【设计模式】单例模式(懒汉式)==>导致线程不安全验证
public class MyBenchmark { public static void main(String[] args) { new Thread(()-> { Singleton instance1 = Singleton.getInstance(); },"t1").start(); new Thread(()-> { Singleton instance2 = Singleton.getInstance(); },"t2").start();原创 2021-05-03 09:25:10 · 80 阅读 · 0 评论