• 单例模式(面试常考)


    在开发过程中会有许多常见的“场景”,针对这些场景,前辈们总结出了一些固定的套路。单例模式就是设计模式中的一种。单例模式能保证某个类在程序中只存在唯一一份实例,而不会创建出多个实例。

    单例模式的实现

    1.饿汉模式

    1. static class Singleton{
    2. private static Singleton instance=new Singleton();//static修饰的变量是类变量,一个类只有一份
    3. public Singleton getInstance(){
    4. return instance;//提供接口
    5. }
    6. private Singleton(){
    7. ;//把构造方法设为private,此时在类的外面,就无法继续new实例
    8. }

    2.懒汉模式

    饿汉模式死在类加载阶段就创建的实例,创建实例的时机过早。而懒汉模式创建实例更迟,需要创建时才创建,效率更高。

    1. static class SingletonLazy{
    2. private static SingletonLazy instance=null;
    3. public static SingletonLazy getInstance(){
    4. if(instance==null){
    5. instance=new SingletonLazy();//需要创建实例的时机,首次调用getInstance()才会触发,后续在调用就会立即返回
    6. }
    7. return instance;
    8. }
    9. private SingletonLazy(){
    10. ;
    11. }
    12. }

    但是上诉代码在多线程环境下由于创建实例即涉及读,又涉及写操作会导致线程不安全。例如t2线程在进行load的时候,t1还没修改完,t2读到还是旧值,t2仍能进行new操作,就会导致创建多份实例。要想t2读到t1修改的值,就需要t2在load要在t1执行完new操作。

    解决方案->加锁

    1. static class SingletonLazy{
    2. private static SingletonLazy instance=null;
    3. public static SingletonLazy getInstance(){
    4. synchronized (SingletonLazy.class) {
    5. if (instance == null) {
    6. instance = new SingletonLazy();//需要创建实例的时机,首次调用getInstance()才会触发,后续在调用就会立即返回
    7. }
    8. }
    9. return instance;
    10. }
    11. private SingletonLazy(){
    12. ;
    13. }
    14. }

    加锁解决了线程安全问题,但是加锁开销也不小,如果像上面代码,导致后续线程已经安全的时候仍然加锁,实际上只需要首次调用时候加锁即可。

    1. static class SingletonLazy{
    2. private static SingletonLazy instance=null;
    3. public static SingletonLazy getInstance(){
    4. if(instance==null) {//判断是否要加锁
    5. synchronized (SingletonLazy.class) {
    6. if (instance == null) {//判断是否要创建实例
    7. instance = new SingletonLazy();//需要创建实例的时机,首次调用getInstance()才会触发,后续在调用就会立即返回
    8. }
    9. }
    10. }
    11. return instance;
    12. }
    13. private SingletonLazy(){
    14. ;
    15. }
    16. }

    你以为上面代码已经没问题?其实还有线程不安全问题->指令重排序

    new操作本质上分为3个步骤

    1.申请内存,得到内存首地址

    2.调用构造方法,来初始化实例

    3.把内存首地址赋值给instance引用

    在单线程中2,3是可以调换位置的,但是在多线程中,如果按照1,3,2执行,在t1线程执行1,3之后,执行2之前。t2线程调用getInstance就会认为Instance非空,直接返回instance,后面对instance进行解引用操作(使用里面的属性,方法)

    解决方案->volatile修饰

    1. static class SingletonLazy{
    2. private volatile static SingletonLazy instance=null;
    3. public static SingletonLazy getInstance(){
    4. if(instance==null) {//判断是否要加锁
    5. synchronized (SingletonLazy.class) {
    6. if (instance == null) {//判断是否要创建实例
    7. instance = new SingletonLazy();//需要创建实例的时机,首次调用getInstance()才会触发,后续在调用就会立即返回
    8. }
    9. }
    10. }
    11. return instance;
    12. }
    13. private SingletonLazy(){
    14. ;
    15. }
    16. }

  • 相关阅读:
    04 MIT线性代数-矩阵的LU分解 Factorization into A=LU
    TP5 中 FIND_IN_SET的使用方法 精准匹配,字段值以“,”隔开查询
    QML自定义电池状进度条
    docker 部署 kafka-ui
    Head First设计模式(阅读笔记)-05.单例模式
    网络安全—黑客技术—自学笔记
    分享一个Java毕业设计项目——叮当书城项目
    代码随想录刷题|LeetCode 1049. 最后一块石头的重量II 494. 目标和 474.一和零
    ubuntu18.04 下海康工业相机hikrobot_camera的使用及问题的解决
    Linux和Windows中的JDK和Tomcat环境变量配置
  • 原文地址:https://blog.csdn.net/weixin_61427900/article/details/126909984