• 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

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

  • 相关阅读:
    SSM框架整合
    docker学习笔记
    DAY26:GetShell专题
    K8S中的pod自动扩容与缩容
    使用LocalForage进行浏览器端数据存储
    离散数学--连通性和矩阵
    ElasticSearch-Head 数据浏览406问题解决
    vue3在setup中获取DOM元素
    理解Kruskal算法的前提----深入理解并查集【超简单~】
    随机数发生器设计(四)
  • 原文地址:https://blog.csdn.net/sunyuhua_keyboard/article/details/132670345