• java 单例模式


    单例模式是最简单的设计模式之一。即一个类负责创建自己的对象,同时确保只有单个对象被创建,提供一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。

    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

    这种方式的特点是线程不安全

    2、加锁,线程安全

    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

    效率很低,因为在绝大部分情况下并不需要同步

    3、饿汉式
    常用,但容易产生垃圾对象

    public class Singleton {
    	private static Singleton instance = new Singleton();
    	private Singleton() {}
    	public static Singleton getInstance() {
    		return this.instance
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4、双检锁/双重校验锁(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

    由于只有在很少的情况下会出现需要同步的情况,所以先通过singleton == null 减少进入的概率,然后使用一个类锁保证线程安全

    5、静态内部类

    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

    静态内部类的加载时机是第一次被引用的时候

    getInstance方法返回的始终是静态对象INSTANCE,当这个方法被调用时,SingleTonHolder才在SingleTon的运行时常量池里,把符号引用替换成了直接引用,这时才真正创建了静态对象INSTANCE

    因此,静态内部类方法在创建过程中是线程安全的,且延迟了单例的实例化

    6、枚举

    public enum Singleton {  
        INSTANCE;  
        public void whateverMethod() {  
        }  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    第 4 章 串(串的堆分配存储实现)
    惊!这么好用的纯html网页模板可还行?偷偷拿去做作业真是绝绝子!!
    CRUD和文件上传下载
    2023-简单点-开启防火墙后,ping显示请求超时;windows共享盘挂在不上
    盘点Golang测试相关库
    LeetCode 热题100——链表专题(一)
    LeetCode 2582. 递枕头
    AX=0和AX=b的解向量线性相关吗?
    论文笔记:Evaluating the Performance of Large Language Models on GAOKAO Benchmark
    数据库表关系详解(一对多、一对一、多对多)
  • 原文地址:https://blog.csdn.net/qq_37831759/article/details/136173499