• java 实现单例模式


    单例模式是一种设计模式,用于确保一个类只有一个实例,并提供一种全局访问该实例的方式。在Java中,可以使用多种方式来实现单例模式,下面整理了几种常见的实现方式。

    1. 饿汉式单例模式(Eager Initialization)

      在类加载时就创建实例,因此是线程安全的。

    public class EagerSingleton {
        private static final EagerSingleton instance = new EagerSingleton();
    
        private EagerSingleton() {}
    
        public static EagerSingleton getInstance() {
            return instance;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. 懒汉式单例模式(Lazy Initialization)

      在首次访问时创建实例,需要考虑多线程安全问题,可以使用双重检查锁定来确保线程安全。

    public class LazySingleton {
        private static volatile LazySingleton instance;
    
        private LazySingleton() {}
    
        public static LazySingleton getInstance() {
            if (instance == null) {
                synchronized (LazySingleton.class) {
                    if (instance == null) {
                        instance = new LazySingleton();
                    }
                }
            }
            return instance;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1. 静态内部类单例模式

      利用静态内部类的类加载机制来实现懒加载和线程安全。

    public class StaticInnerClassSingleton {
        private StaticInnerClassSingleton() {}
    
        private static class SingletonHolder {
            private static final StaticInnerClassSingleton instance = new StaticInnerClassSingleton();
        }
    
        public static StaticInnerClassSingleton getInstance() {
            return SingletonHolder.instance;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. 枚举单例模式

      利用枚举类型的特性,实现简单且线程安全的单例模式。

    public enum EnumSingleton {
        INSTANCE;
    
        // 可以在枚举中添加其他方法和属性
        public void doSomething() {
            // ...
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. 双重检查锁定(Double-Checked Locking)

      通过双重检查来实现懒加载和线程安全。

    public class DoubleCheckedLockingSingleton {
        private static volatile DoubleCheckedLockingSingleton instance;
    
        private DoubleCheckedLockingSingleton() {}
    
        public static DoubleCheckedLockingSingleton getInstance() {
            if (instance == null) {
                synchronized (DoubleCheckedLockingSingleton.class) {
                    if (instance == null) {
                        instance = new DoubleCheckedLockingSingleton();
                    }
                }
            }
            return instance;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    以上是一些常见的单例模式实现方式。你可以根据项目需求和线程安全性要求选择适当的方式来实现单例模式。在多数情况下,饿汉式和静态内部类单例是推荐的方式,因为它们既线程安全又具备懒加载特性。如果需要在单例中添加其他方法和属性,可以考虑使用枚举单例。

  • 相关阅读:
    如何在线批量将PDF转换成JPG格式
    IaC:实现持续交付和 DevOps 自动化的关键
    java计算机毕业设计小区物业管理系统演示录像2020源程序+mysql+系统+lw文档+远程调试
    C++回顾从入门开始
    基于vue+node.js+zigbee的蔬菜大棚温湿度物联网监控系统
    Simulink仿真----升压(Boost)变换器
    MacOS系统电脑怎么彻底清理系统垃圾注册表App Cleaner可以深度清理吗
    C/C++简单计算器 2019年12月电子学会青少年软件编程(C/C++)等级考试一级真题答案解析
    【UE5】物体沿样条线移动
    HashMap的put方法的源码分析(Java)
  • 原文地址:https://blog.csdn.net/sunyuhua_keyboard/article/details/132670345