单例模式_饿汉模式_懒汉模式(含线程安全写法)

前言

某个类在程序中只存在唯一一份实例,叫做单例模式。
 

目录

前言

一、饿汉模式

二、懒汉模式

(一)单线程写法

(二)线程安全写法

(三)线程安全和不安全写法的区别

结语


一、饿汉模式

程序启动,类加载之后,立即创建实例,叫做饿汉模式。

代码:

class Singleton{
    private static Singleton singleton = new Singleton();

    public static Singleton getSingleton() {
        return singleton;
    }
    
    private Singleton(){}
}

关于代码的注意事项,如图:

二、懒汉模式

在第一次使用实例再创建对象,否则就不创建实例对象,叫做懒汉模式。

(一)单线程写法

代码:

class SingletonLazy{
    private static SingletonLazy singletonLazy = null;

    public static SingletonLazy getSingletonLazy() {
        if(singletonLazy == null){
            singletonLazy = new SingletonLazy();
        }
        return singletonLazy;
    }

    private SingletonLazy(){}
}

关于代码的注意事项:

 

(二)线程安全写法

分析懒汉模式的线程不安全写法:

当多个线程执行时,有如下执行语句的可能:

 故,在这种情况下就有两个SingletonLazy 的实例对象的创建,违背了最初的意图,所以此前的写法是线程不安全的。

解决办法是:加锁。

1)根据代码分析,这几条语句最好处理成原子的:


2)故把锁加在 if 语句的外面:


3)考虑到加锁的开销,每次调用该 getSingletonLazy() ,都需要先加锁,再进行判断,所以可以采取在加锁前就进行判断,加锁后也再进行判断,避免重复加锁:


 4)避免指令重排序和内存可见性问题,在类变量前加上 volatile :


完整代码是:

class SingletonLazy{
    volatile private static SingletonLazy singletonLazy = null;

    public static SingletonLazy getSingletonLazy() {
        if(singletonLazy == null) {
            synchronized (SingletonLazy.class) {
                if (singletonLazy == null) {
                    singletonLazy = new SingletonLazy();
                }
            }
        }
        return singletonLazy;
    }

    private SingletonLazy(){}
}

(三)线程安全和不安全写法的区别

  1. 加锁;
  2. 双重 if ;
  3. volatile.

结语

这篇博客如果对你有帮助,给博主一个免费的点赞以示鼓励,欢迎各位🔎点赞👍评论收藏⭐,谢谢!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Y君的进化史

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

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

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

打赏作者

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

抵扣说明:

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

余额充值