EnseHeiKe
Ense
采纳率60%
2019-11-14 17:29 阅读 284
已采纳

设计模式中单例模式的双重验证模式的问题

双重验证代码:
/**

  • 双重验证懒汉模式(线程安全)
    *
    */
    public class LazyAgainSingletonPattern {

    private static volatile LazyAgainSingletonPattern pattern;

    private LazyAgainSingletonPattern(){

    }

    public static LazyAgainSingletonPattern getInstance() throws InterruptedException {
    if(pattern == null){
    Thread.sleep(1000);
    synchronized (LazyAgainSingletonPattern.class){
    if(pattern == null){
    pattern = new LazyAgainSingletonPattern();
    }
    }
    }
    return pattern;
    }
    }
    可以改为单重验证会有什么问题呢?
    如:public class LazyAgainSingletonPattern {

    private static volatile LazyAgainSingletonPattern pattern;

    private LazyAgainSingletonPattern(){

    }

    public static LazyAgainSingletonPattern getInstance() throws InterruptedException {
    Thread.sleep(1000);
    synchronized (LazyAgainSingletonPattern.class){
    if(pattern == null){
    pattern = new LazyAgainSingletonPattern();
    }
    }
    return pattern;
    }
    }
    少了一重验证,多线程下测试时结果都一样,都是单例对象
    测试代码:
    public class Test {

    public static void main(String[] args) {
    for(int i = 0;i < 10; i++){
    new Thread(()->{
    LazyAgainSingletonPattern singletonPattern = null;
    try {
    singletonPattern = LazyAgainSingletonPattern.getInstance();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println(singletonPattern);
    }).start();
    }
    }
    }
    输出结果都一样,请问双重验证为什么不能改为单重验证呢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    msx985211 创业的鱼 2019-11-14 17:59

    就单例本身来讲,synchronized (LazyAgainSingletonPattern.class)已经能够保证了,我想你的疑惑是为啥在synchronized (LazyAgainSingletonPattern.class)这一句外层还要加一个是否为空的判断,需要说明下:懒加载的双重校验,外面一层是否为空的判断,目的是为了减少并发访问时线程阻塞所带来的问题,如果不为空则不再竞争锁。
    我的博客,了解一下https://blog.csdn.net/msx985211/article/details/102715312

    点赞 1 评论 复制链接分享
  • qq_36601979 zyydomain 2019-11-14 18:16

    你可以了解一下指令重排

    点赞 评论 复制链接分享

相关推荐