单例模式:饿汉式、懒汉式;线程安全的单例模式创建的6种方式

单例模式

单例模式Singleton是一种创建型模式,指某个采用Singleton单例模式,则这个类在同一个

JVM上,只能产生一个实例供外部访问,并且仅提供一个全局的访问方式。

懒汉式

懒汉式线程不安全

java 复制代码
public class Singleton1 {
	private static Singleton1 instance;
	
	// 构造方法私有化!!!
	private Singleton1() {};
	
	public static Singleton1 getInstance() {
		if(instance == null) {
			instance = new Singleton1();
		}
		return instance;
	}
}

饿汉式

饿汉式线程安全

java 复制代码
public class Singleton2 {
	private static final Singleton2 instance = new Singleton2();
	// 构造方法私有化
	private Singleton2() {};

	public static Singleton2 getInstance() {
		return instance;
	}
}

线程安全的单例模式

  1. 饿汉式线程安全
  2. 全局锁线程安全
  3. 静态代码块创建单例对象线程安全
  4. 双重校验锁实现线程安全
  5. 静态内部类实现线程安全的单例模式
  6. 内部枚举类实现线程安全
  • 全局锁

    java 复制代码
    public class Singleton3 {
    	private static Singleton3 instance;
    
    	// 构造方法私有化
    	private Singleton3() {};
    
    	// 通过synchronized添加全局锁实现线程安全
    	public static synchronized Singleton3 getInstance() {
    		if (instance == null) {
    			instance = new Singleton3();
    		}
    		return instance;
    	}
    }
  • 静态代码块

    java 复制代码
    public class Singleton4 {
    	private static final Singleton4 instance;
    	
    	// 静态代码块创建单例对象
    	static {
    		instance = new Singleton4();
    	}
    	
    	// 构造方法私有化
    	private Singleton4(){}
      
    	public static Singleton4 getInstance() {
    		return instance;
    	}
    }
  • 双重校验锁

    java 复制代码
    public class Singleton5 {
    
    	private static Singleton5 instance;
    
    	private Singleton5() {
    	}
    
    	public static Singleton5 getInstance() {
    		if (instance == null) {
    			synchronized (Singleton5.class) {
    				if (instance == null) {
    					instance = new Singleton5();
    				}
    			}
    		}
    		return instance;
    	}
    }
  • 静态内部类

    java 复制代码
    public class Singleton6 {
    	// 构造方法私有化
    	private Singleton6() {};
    	
    	// 静态内部类
    	private static class SingletonHolder{
    		// 创建单例对象
    		private static Singleton6 instance = new Singleton6();
    	}
    	public static Singleton6 getInstance() {
    		return Singleton6.SingletonHolder.instance;
    	}
    }
  • 静态枚举类

    java 复制代码
    public class Singleton7 {
    	private Singleton7() {}
    	
    	// 内部枚举类
    	enum SingletonEnum{
    		INSTANCE;
    		
    		// 单例对象
    		private Singleton7 instance;
        
    		private SingletonEnum() {
    			instance = new Singleton7();
    		}
        
    		private static Singleton7 getInstance() {
    			return SingletonEnum.INSTANCE.instance;
    		}
    	}
    }	
相关推荐
风123456789~6 小时前
【架构专栏】第18章 安全架构设计 1/3
安全·架构·安全架构
天远API6 小时前
零信任架构实战:基于天远运营商三要素简版V即时版查询构建自动化司乘安全绑定网关
人工智能·安全·架构·自动化
linux_cfan12 小时前
videojs v10 源代码系列解读:34 · `createComposition`:类型安全的冲突检测
前端·安全
hasty12 小时前
传完再检查为何太晚?LXD 迁移流的文件写入边界
安全·系统安全
每天题库12 小时前
架子工脚手架工题库:安全操作考点与模拟题练习指南
学习·安全·考试·题库·考证
白帽攻防录13 小时前
SRC 挖洞:WSO2 JWT 算法混淆绕过深度复盘,CVE-2026-5430 不支持的算法怎么变成管理员
网络·算法·安全·网络安全·jwt
正经教主14 小时前
【FDE系列】阶段3:Day 58:Prompt 安全 — 注入、越狱与防护
网络·人工智能·安全·prompt·fde
hasty14 小时前
配置禁止重定向,为何请求仍继续?Axios Fetch 适配器的安全契约
安全
jimmyleeee15 小时前
大模型安全之三十六:大模型数据管理----从投毒防御到偏见治理的完整框架
人工智能·安全
姜鱼问生16 小时前
三层防火墙排查:Nginx 配好了浏览器还打不开
nginx·安全