• Java笔记——Singleton单例模式


    Java笔记——Singleton单例模式

    本次笔记的目录

    Singleton单例模式

    参考内容:Singleton单例模式
    关键代码:构造函数是私有的。
    几种常见实现方式:

    1. 懒汉式(线程不安全版本)
    public class Singleton {  
        private static Singleton instance;  
        private Singleton (){}  
      
        public static Singleton getInstance() {  
            if (instance == null) {  
                instance = new Singleton();  
            }  
            return instance;  
        }  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. 懒汉式(线程安全版本)
    public class Singleton {  
        private static Singleton instance;  
        private Singleton (){}  
        public static synchronized Singleton getInstance() {  
            if (instance == null) {  
                instance = new Singleton();  
            }  
            return instance;  
        }  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1. 饿汉式
    public class Singleton {  
        private static Singleton instance = new Singleton();  
        private Singleton (){}  
        public static Singleton getInstance() {  
        return instance;  
        }  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    1. 双检锁/双重校验锁(DCL,即 double-checked locking)
    public class Singleton {  
        private volatile static Singleton singleton;  
        private Singleton (){}  
        public static Singleton getSingleton() {  
        if (singleton == null) {  
            synchronized (Singleton.class) {  
                if (singleton == null) {  
                    singleton = new Singleton();  
                }  
            }  
        }  
        return singleton;  
        }  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    1. 登记式/静态内部类
    public class Singleton {  
        private static class SingletonHolder {  
        private static final Singleton INSTANCE = new Singleton();  
        }  
        private Singleton (){}  
        public static final Singleton getInstance() {  
            return SingletonHolder.INSTANCE;  
        }  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. 枚举
    public enum Singleton {  
        INSTANCE;  
        public void whateverMethod() {  
        }  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    使用场景:
    1、要求生产唯一序列号。
    2、WEB 中的计数器,不用每次刷新都在数据库里加一次,用单例先缓存起来。
    3、创建的一个对象需要消耗的资源过多,比如 I/O 与数据库的连接等。
    注意事项:getInstance() 方法中需要使用同步锁 synchronized (Singleton.class) 防止多线程同时进入造成 instance 被多次实例化。

    总结

    在经过java语言的变更,出现了“茴”字的四种写法的既视感…
    建议使用第 3 种饿汉方式(经典)。可以尝试使用第 6 种枚举方式(较新)。如果有其他特殊的需求,可以考虑使用第 4 种双检锁方式。

  • 相关阅读:
    centos使用root用户登录
    R语言详解二
    LeetCode 0304. 二维区域和检索 - 矩阵不可变
    GLTF-pipeline
    科目一扣分
    告诉你,项目管理就用这一页纸
    C++算法前缀和的应用:得分最高的最小轮调的原理、源码及测试用例
    javaScript:DOM元素的获取(静态/动态获取)
    1625. 执行操作后字典序最小的字符串-枚举
    MyBatis-Plus快速入门
  • 原文地址:https://blog.csdn.net/qq_40509784/article/details/126576675