单例模式之懒汉的并发问题,只需要添加一个 synchronized 就可以解决了

该博客讨论了在并发环境中懒汉式单例模式可能遇到的问题,通过实例展示了当多个线程同时调用 getInstance 方法时,可能导致多次实例化。博主提出,只需在 getInstance 方法上添加 synchronized 关键字,即可确保线程安全,实现单例的正确创建。
摘要由CSDN通过智能技术生成

复现并发问题:

package review.bank;

/**
 * Created by kodulf on 2017/2/26.
 */
public class SingleInstanceLazyMan {

    private SingleInstanceLazyMan(){

    }

    static SingleInstanceLazyMan singleInstanceLazyMan ;

    //synchronized 如果不添加,那么会出现三个进程一起调度的情况
    public static SingleInstanceLazyMan getInstance(){
        if(singleInstanceLazyMan==null){
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());

            singleInstanceLazyMan = new SingleInstanceLazyMan();

            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());

        }

        return singleInstanceLazyMan;
    }

}


新建三个进程去调用这个单例的getInstance方法

    public static  void testSingleInstance(){
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                SingleInstanceLazyMan.getInstance();
            }
        });

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                SingleInstanceLazyMan.getInstance();
            }
        });
        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                SingleInstanceLazyMan.getInstance();
            }
        });

        thread1.start();
        thread2.start();
        thread3.start();
    }


运行结果:会发现都调用了if == null的那个判断了,就是执行了里面的程序了

before new Thread-2
before new Thread-2
before new Thread-2
before new Thread-0
before new Thread-1
before new Thread-1
before new Thread-1
before new Thread-1
before new Thread-1
before new Thread-1
before new Thread-1
before new Thread-1
before new Thread-1
before new Thread-1
before new Thread-1
before new Thread-1
before new Thread-1
before new Thread-1
before new Thread-1
before new Thread-1
before new Thread-1
before new Thread-1
before new Thread-1
before new Thread-1
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
before new Thread-2
before new Thread-2
before new Thread-2
before new Thread-2
before new Thread-2
before new Thread-2
before new Thread-2
before new Thread-2
before new Thread-2
before new Thread-2
before new Thread-2
before new Thread-2
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-1
after new Thread-1
after new Thread-1
after new Thread-1
after new Thread-1
after new Thread-1
after new Thread-1
after new Thread-1
after new Thread-1
after new Thread-1
after new Thread-1
after new Thread-1
after new Thread-1
before new Thread-2
before new Thread-2
before new Thread-2
before new Thread-2
before new Thread-2
after new Thread-2
after new Thread-2
after new Thread-2
after new Thread-2
after new Thread-2
after new Thread-2
after new Thread-2
after new Thread-1
after new Thread-1
after new Thread-1
after new Thread-1
after new Thread-1
after new Thread-1
after new Thread-1
after new Thread-1
after new Thread-2
after new Thread-2
after new Thread-2
after new Thread-2
after new Thread-2
after new Thread-2
after new Thread-2
after new Thread-2
after new Thread-2
after new Thread-2
after new Thread-2
after new Thread-2
after new Thread-2
after new Thread-2

Process finished with exit code 0


要解决这个问题:

只需要添加一个单词,synchronized 就可以了

package review.bank;

/**
 * Created by zhangyinshan on 2017/2/26.
 */
public class SingleInstanceLazyMan {

    private SingleInstanceLazyMan(){

    }

    static SingleInstanceLazyMan singleInstanceLazyMan ;

    //synchronized 如果不添加,那么会出现三个进程一起调度的情况
    public static synchronized SingleInstanceLazyMan getInstance(){
        if(singleInstanceLazyMan==null){
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());
            System.out.println("before new "+Thread.currentThread().getName());

            singleInstanceLazyMan = new SingleInstanceLazyMan();

            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());
            System.out.println("after new "+Thread.currentThread().getName());

        }

        return singleInstanceLazyMan;
    }

}


运行结果:只调用了一次,这个就是我们想要的

before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
before new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0
after new Thread-0

Process finished with exit code 0






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值