• 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

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

  • 相关阅读:
    Redis基础特性及应用练习-php
    基于Python的海贼王知识图谱构建设计
    8.09 Day38---Java基础面试题
    无监督学习的集成方法:相似性矩阵的聚类
    【Verilog】Verilog设计进阶
    ChatGPT插件开发实战
    数据库插入数据
    Linux实操篇-定时任务调度
    纯CSS 斑马投影文字
    3D打印机的使用教程( 超详细 )
  • 原文地址:https://blog.csdn.net/sunyuhua_keyboard/article/details/132670345