总结单例模式的写法(在线程安全的情况下)

目录

1 饿汉模式

2 懒汉模式 

3 分析


啥是设计模式? 设计模式好⽐象棋中的 "棋谱". 红⽅当头炮, ⿊⽅⻢来跳. 针对红⽅的⼀些⾛法, ⿊⽅应招的时候有⼀ 些固定的套路. 按照套路来⾛局势就不会吃亏. 软件开发中也有很多常⻅的 "问题场景". 针对这些问题场景, ⼤佬们总结出了⼀些固定的套路. 按照这 个套路来实现代码, 也不会吃亏.

单例模式能保证某个类在程序中只存在唯⼀⼀份实例, ⽽不会创建出多个实例.

这⼀点在很多场景上都需要. ⽐如 JDBC 中的 DataSource 实例就只需要⼀个.

单例模式具体的实现⽅式有很多. 最常⻅的是 "饿汉" 和 "懒汉" 两种

1 饿汉模式

饿汉模式(迫切) 程序启动, 类加载之后,立即创建出实例.

//单例模式 (饿汉模式)

class Singleton {

    private static Singleton instance = new Singleton();

    public static Singleton getInstance() {
        return instance;
    }
    // 做出一个限制, 禁止别人去 new 这个实例!!
    private Singleton() {}

}
public class Demo16 {
    public static void main(String[] args) {
        Singleton s1 = Singleton.getInstance();
        Singleton s2 = Singleton.getInstance();
        System.out.println(s1 == s2);
        // Singleton s3 = new Singleton();
        // System.out.println(s1 == s3);
    }
}

2 懒汉模式 

懒汉模式(延时) 则是在第一次使用实例的时候, 再创建. 否则能不创建就不创建.

// 单例模式(懒汉模式)
class SingletonLazy {
    private static SingletonLazy instance = null;

    public static SingletonLazy getInstance() {
        if (instance == null) {
            instance = new SingletonLazy();
        }
        return instance;
    }

    private SingletonLazy() {}
}

public class Demo17 {
    public static void main(String[] args) {
        SingletonLazy s1 = SingletonLazy.getInstance();
        SingletonLazy s2 = SingletonLazy.getInstance();
        System.out.println(s1 == s2);
    }
}

3 分析

那么上述两种模式, 谁是线程安全的. 换句话说, 在多线程调用getInstance的情况下, 哪个代码是线程安全的(不会有bug).

先分析饿汉模式.

再来看懒汉模式.

那么如何解决上述问题呢?

首先想到的就是加锁, 我们要让判断和赋值操作绑在一起.

但是加锁是一个成本较高的操作, 可能会引起阻塞等待. 

加锁的基本原则, 应该是非必要, 不加锁. 不能无脑加锁. 如果无脑加锁, 就会导致程序执行效率受到影响.

Vector, HashTable, StringBuffer ....都是在关键方法上写了synchronized .
无论是单线程使用,还是多线程使用, 无论是否场景存在线程安全问题, 都是会加锁的, 也都会影响到效率, 都不太建议使用.

但是代码还是有一些问题.

经过上述的优化中后, 线程安全的懒汉模式代码就成了这个样子.

// 单例模式(懒汉模式)
class SingletonLazy {
    private static volatile SingletonLazy instance = null;
    //instance是null, 就说明是首次调用, 就需要加锁. 如果是非null, 就说明是后续调用,就不需要加锁
    public static SingletonLazy getInstance() {
        if (instance == null) {
            synchronized (SingletonLazy.class) {
                if (instance == null) {
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }

    private SingletonLazy() {}
}

public class Demo17 {
    public static void main(String[] args) {
        SingletonLazy s1 = SingletonLazy.getInstance();
        SingletonLazy s2 = SingletonLazy.getInstance();
        System.out.println(s1 == s2);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

早点睡觉1.0

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

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

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

打赏作者

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

抵扣说明:

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

余额充值