单例模式:饿汉式、懒汉式;线程安全的单例模式创建的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;
    		}
    	}
    }	
相关推荐
workflower31 分钟前
用硬件换时间”与“用算法降成本”之间的博弈
人工智能·算法·安全·集成测试·无人机·ai编程
OPHKVPS1 小时前
Anthropic 为 Claude Code 推出“自动模式”:AI 编码工具迈向更高自主性
网络·人工智能·安全·ai
志栋智能1 小时前
超自动化运维的终极目标:让系统自治运行
运维·网络·人工智能·安全·自动化
阿里巴巴中间件2 小时前
Nacos 3.2 Skill Registry 正式版发布,让 AI 能力在企业更安全、可控落地
人工智能·安全
大囚长3 小时前
AI安全研究员现状
人工智能·安全
安全渗透Hacker4 小时前
Inspectio:Python双引擎驱动,轻量化日志敏感信息安全审查工具
安全·安全性测试
星幻元宇VR5 小时前
VR动感科普单车:让交通安全教育更真实、更有效
科技·学习·安全·生活·vr
攻城狮在此6 小时前
华三交换机ACL配置(封禁内网高危端口)
网络·安全
123过去6 小时前
hashid使用教程
linux·网络·测试工具·安全
cdprinter6 小时前
信刻安全加密光盘,保障光盘保密安全
网络·安全·自动化