单例模式(Singleton Pattern)

1 什么是单例模式?

单例模式就是保证运行过程中某个类的实例对象只有一个,例如在 JavaWeb 程序中,service 层的对象其实主要就是提供服务,全局获取一个对象足矣,一般情况下不需要重复定义对象。但是我最初学习的时候也是,在每个 controller 中 new service 层的对象实例,这样会造成资源的浪费;
单例模式属于创造型模式,用于实例化对象。

2 几种单例模式的实现

单例模式的实现有好几种,甚至有大佬列举了 8 种,详细情况如下:
java单例模式——详解JAVA单例模式及8种实现方式_单例模式总结-CSDN博客

个人之前学习的只知道以下几种,真是学无止境啊!

2.1 饿汉单例(线程安全)

直接在类加载的时候就 new 出对象

package se.wangs.singleton;

/**
 * -- coding: UTF-8 -- *
 *
 * @author wangs
 * @date 2023/12/6 11:23
 * @description 饿汉单例
 */
public class Singleton1 {
   
    private static final Singleton1 SINGLETON_INSTANCE = new Singleton1();

    private Singleton1() {
   }
    
    public static Singleton1 getSingletonInstance() {
   
        return SINGLETON_INSTANCE;
    }
}

2.2 饿汉单例(线程不安全)

当多个线程同时访问可能会进入 if 语句,造成实例化多个实例;

package se.wangs.singleton;

/**
 * -- coding: UTF-8 -- *
 *
 * @author wangs
 * @date 2023/12/6 11:27
 * @description 懒汉单例(线程不安全1)
 */
public class Singleton2 {
   
    private static Singleton2 singletonInstance;
    
    private Singleton2() {
   }
    
    private static Singleton2 getSingletonInstance() {
   
        if (singletonInstance == null) {
   
            singletonInstance = new Singleton2();
        }
        return singletonInstance;
    }
}

2.3 懒汉单例(锁方法)

这种方式是线程安全的,但是直接锁住方法的线程较低

package se.wangs.singleton;

/**
 * -- coding: UTF-8 -- *
 *
 * @author wangs
 * @date 2023/12/6 11:27
 * @description 懒汉单例(锁方法)
 */
public class Singleton3 {
   
    private static Singleton3 singletonInstance;

    private Singleton3() {
   }

    private static synchronized Singleton3 getSingletonInstance() {
   
        if (singletonInstance == null) {
   
            singletonInstance = new Singleton3();
        }
        return singletonInstance;
    }
}

2.4 懒汉单例(双重检查)

线程安全

package se.wangs.singleton;

/**
 * -- coding: UTF-8 -- *
 *
 * @author wangs
 * @date 2023/12/6 11:33
 * @description 饿汉单例(双重检查)
 */
public class Singleton4 {
   
    private volatile static Singleton4 singletonInstance;

    private Singleton4() {
   }

    private static synchronized Singleton4 getSingletonInstance() {
   
        if (singletonInstance == null) {
   
            synchronized (Singleton4.class) {
   
                if (singletonInstance == null) {
   
                    singletonInstance = new Singleton4();
                }
            }
        }
        return singletonInstance;
    }
}

3 总结

3.1 单例模式的关键

(1)私有化构造器:使得该类只能在内部实例化,外部不能实例化;
(2)保证内部能实例化出一个对象,并且仅实例化一次;
(3)考虑线程安全,需要注意加锁;

3.2 volatile 关键字

参考大佬博客
Java并发常见面试题总结(中)
该关键字可以保证可见性,修饰的变量,在每次访问的时候,都要去内存中获取最新的值

3.3 单例模式在 Spring 中的应用

spring 中 bean 的管理默认是使用的单例模式,即 scope 属性默认是 "scope=singleton"

相关推荐

  1. .NET 设计模式模式SingletonPattern

    2023-12-07 07:28:03       38 阅读
  2. 模式模板

    2023-12-07 07:28:03       43 阅读
  3. 模式【C#】

    2023-12-07 07:28:03       54 阅读
  4. python模式

    2023-12-07 07:28:03       63 阅读
  5. 模式详解

    2023-12-07 07:28:03       61 阅读
  6. 模式学习

    2023-12-07 07:28:03       48 阅读
  7. 模式(C++)

    2023-12-07 07:28:03       51 阅读
  8. 设计模式

    2023-12-07 07:28:03       63 阅读
  9. 【C++ 模式

    2023-12-07 07:28:03       65 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-07 07:28:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-07 07:28:03       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-07 07:28:03       87 阅读
  4. Python语言-面向对象

    2023-12-07 07:28:03       96 阅读

热门阅读

  1. Golang实践录:读取toml配置

    2023-12-07 07:28:03       55 阅读
  2. ElasticSearch 了解文本相似度 TF-IDF吗?

    2023-12-07 07:28:03       47 阅读
  3. Go语言语法上的一些主要特点(1)

    2023-12-07 07:28:03       51 阅读
  4. 人工智能对现代生活的影响

    2023-12-07 07:28:03       49 阅读
  5. Apache Ofbiz XML-RPC RCE漏洞复现(CVE-2023-49070)

    2023-12-07 07:28:03       49 阅读
  6. 超越节点引擎临界:华为云NES颠覆游戏规则

    2023-12-07 07:28:03       59 阅读
  7. Linux网卡命名规则

    2023-12-07 07:28:03       54 阅读
  8. 继承与派生(1)

    2023-12-07 07:28:03       55 阅读
  9. MATLAB基础应用精讲-【数模应用】深度学习杂谈

    2023-12-07 07:28:03       58 阅读
  10. Spacemesh、Kaspa和Chia的全面对比!

    2023-12-07 07:28:03       62 阅读