当前位置: 首页 > news >正文

【设计模式】 单例模式(单例模式哪几种实现,如何保证线程安全,反射破坏单例模式)

单例模式

作用:单例模式的核心是保证一个类只有一个实例,并且提供一个访问实例的全局访问点。

实现方式优缺点
饿汉式线程安全,调用效率高 ,但是不能延迟加载
懒汉式线程安全,调用效率不高,能延迟加载
双重检测锁在懒汉式的基础上解决并发问题
静态内部类线程安全,资源利用率高,可以延时加载
枚举单例线程安全,调用效率高,但是不能延迟加载

饿汉式

在类加载的时候立即实例化对象,实现的步骤是先私有化构造方法,对外提供唯一的静态入口方法

public class SingletonInstance1 {private byte[] b1 = new byte[1024*1024];private byte[] b2 = new byte[1024*1024];private byte[] b3 = new byte[1024*1024];// 声明此类型的变量,并实例化,当该类被加载的时候就完成了实例化并保存在了内存中private final static SingletonInstance1 instance = new SingletonInstance1();// 私有化所有的构造方法,防止直接通过new关键字实例化private SingletonInstance1(){}// 对外提供一个获取实例的静态方法public static SingletonInstance1 getInstance(){return instance;}
}

在类加载时直接创建对象可能会造成空间的浪费

懒汉式

public class SingletonInstance2 {// 声明此类型的变量,但没有实例化private static SingletonInstance2 instance = null;// 私有化所有的构造方法,防止直接通过new关键字实例化private SingletonInstance2(){}// 对外提供一个获取实例的静态方法public static SingletonInstance2 getInstance(){if(instance == null){// 当instance不为空的时候才实例化instance = new SingletonInstance2();}return instance;}
}

外部调用getInstance()方法时才会创建对象(判断对象是否存在),但是不能保证多线程并发的情况下的线程安全,所以就出现了双重检测锁模式

双重检测锁模式

public class SingletonInstance3 {// 声明此类型的变量,但没有实例化,防止指令重排private volatile static SingletonInstance3 instance;// 私有化所有的构造方法,防止直接通过new关键字实例化private SingletonInstance3(){}// 对外提供一个获取实例的静态方法public static SingletonInstance3 getInstance(){if(instance == null){synchronized (SingletonInstance3.class){if(instance == null){// 当instance不为空的时候才实例化instance = new SingletonInstance3();/*1.分配内存空间2.执行构造法法,初始化对象3.把这个对象指向这个空间如果不加volatile 会执行重排序 1 3 2*/}}}return instance;}
}

静态内部类

public class SingletonInstance4 {// 静态内部类public static class SingletonClassInstance{// 声明外部类型的静态常量public static final SingletonInstance4 instance = new SingletonInstance4();}// 私有化构造方法private SingletonInstance4(){}// 对外提供的唯一获取实例的方法public static SingletonInstance4 getInstance(){return SingletonClassInstance.instance;}
}

枚举

public enum EnumSingle {INSTANCE;public EnumSingle getInstance(){return INSTANCE;}
}

如何保证线程安全

推荐使用 静态内部类 或者 双重检测锁 配合volatile使用

反射破坏单例模式

代码如下

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;public class LazyMan {private static boolean jiamibiaozhi = false;  // 加密标志位// 私有化所有的构造方法,防止直接通过new关键字实例化private LazyMan(){synchronized (LazyMan.class){if(!jiamibiaozhi){  // 防止反射破坏单例jiamibiaozhi = true;}else {throw new RuntimeException("不能试图使用反射破坏异常");}}System.out.println(Thread.currentThread().getName() +"LazyMan");}// 声明此类型的变量,但没有实例化, volatile防止指令重排private volatile static LazyMan instance;// 对外提供一个获取实例的静态方法public static LazyMan getInstance(){if(instance == null){synchronized (LazyMan.class){if(instance == null){// 当instance不为空的时候才实例化instance = new LazyMan();/*1.分配内存空间2.执行构造法法,初始化对象3.把这个对象指向这个空间如果不加volatile 会执行重排序 1 3 2*/}}}return instance;}// 反射破环单列public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {// LazyMan lazyMan = LazyMan.getInstance();Constructor<LazyMan> declaredConstructor = LazyMan.class.getDeclaredConstructor(null); // 获取空参构造器declaredConstructor.setAccessible(true); // 暴力反射,设置权限,无视私有构造器LazyMan lazyMan1 = declaredConstructor.newInstance(); // 通过空参构造器创建对象LazyMan lazyMan2 = declaredConstructor.newInstance();System.out.println(lazyMan1);System.out.println(lazyMan2);}
}

反射不能破坏枚举,见源码
在这里插入图片描述

http://www.lryc.cn/news/522681.html

相关文章:

  • T-SQL语言的数据库交互
  • 【Linux系统】Ext系列磁盘文件系统二:引入文件系统(续篇)
  • 慧集通(DataLinkX)iPaaS集成平台-业务建模之域
  • 【机器学习实战】kaggle 欺诈检测---使用生成对抗网络(GAN)解决欺诈数据中正负样本极度不平衡问题
  • android wifi framework与wpa_supplicant的交互
  • 初学stm32 --- flash模仿eeprom
  • 使用C语言实现栈的插入、删除和排序操作
  • C语言程序环境和预处理详解
  • 基于机器学习随机森林算法的个人职业预测研究
  • 三种文本相似计算方法:规则、向量与大模型裁判
  • Python语言的计算机基础
  • Dify应用-工作流
  • 02.02、返回倒数第 k 个节点
  • Linux手写FrameBuffer任意引脚驱动spi屏幕
  • 怎么修复损坏的U盘?而且不用格式化的方式!
  • 语音技术在播客领域的应用(2)
  • 【Linux】应用层自定义协议与序列化
  • 深度学习中的张量 - 使用PyTorch进行广播和元素级操作
  • gitignore忽略已经提交过的
  • h5使用video播放时关掉vant弹窗视频声音还在后台播放
  • Widows搭建sqli-labs
  • 为AI聊天工具添加一个知识系统 之46 蒙板程序设计(第一版):Facet六边形【意识形态:操纵】
  • ASP.NET Core WebApi接口IP限流实践技术指南
  • 文件移动工具 (File Mover)
  • PTA L1-039 古风排版
  • Docker 镜像加速的配置
  • 简历_使用优化的Redis自增ID策略生成分布式环境下全局唯一ID,用于用户上传数据的命名以及多种ID的生成
  • PHP的HMAC_SHA1和HMAC_MD5算法方法
  • 二进制/源码编译安装mysql 8.0
  • 2025-1-15-十大经典排序算法 C++与python