设计模式。双重检查单例(优化到极致完美),解决单例懒汉式的线程不安全

上文讲到

单纯的加锁就可以

package com.yzdzy.design.singleton;

/**
 * 加锁

 */
public class Mgr04 {
    private static Mgr04 mInstance;


    private Mgr04() {
    }

    public static synchronized Mgr04 getInstance() {

        if (mInstance == null) {
            // 两个线程里面 会创建多个实例。
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            mInstance = new Mgr04();
        }
        return mInstance;
    }
    public void m() {
        System.out.println("m");
    }
    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                System.out.println(Mgr04.getInstance().hashCode());
            }).start();
        }
    }

}

but 效率低了对吗

那么有人尝试通过锁同步代码的方式去解决

package com.yzdzy.design.singleton;


public class Mgr05 {
    private static Mgr05 mInstance;


    private Mgr05() {
    }

    public static Mgr05 getInstance() {

        if (mInstance == null) {
            // 妄图通过减小同步代码库的方式提高效率,然后不可行
            synchronized (Mgr05.class) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                mInstance = new Mgr05();
            }
        }

        return mInstance;

    }

    public void m() {
        System.out.println("m");
    }

    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                System.out.println(Mgr05.getInstance().hashCode());
            }).start();
        }
    }

}

解决了吗

没有。

> Task :Mgr05.main()
1288032892
1696172314
1098833185
471895473
334817404
541982512
662136803
1722732360
1133601195
1313614725

因为他还是持有了懒加载的间隔  和最初的懒加载拥有相同的诟病

解决方案应势而生

双重检查

package com.yzdzy.design.singleton;

/**
 * 懒汉式
 * lazy loading
 * 优点:按需初始化,什么时候用什么时候才初始化
 * 缺点:线程不安全
 */
public class Mgr06 {
    //jit volatile 解决指令重排
    private static volatile Mgr06 mInstance;


    private Mgr06() {
    }

    public static Mgr06 getInstance() {

        if (mInstance == null) {
            // 妄图通过减小同步代码库的方式提高效率,然后不可行
            synchronized (Mgr06.class) {
                if (mInstance == null) {
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    mInstance = new Mgr06();
                }
            }
        }

        return mInstance;

    }

    public void m() {
        System.out.println("m");
    }

    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                System.out.println(Mgr06.getInstance().hashCode());
            }).start();
        }
    }

}
> Task :Mgr06.main()
1288032892
1288032892
1288032892
1288032892
1288032892
1288032892
1288032892
1288032892
1288032892
1288032892

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值