当前位置:网站首页>单例模式
单例模式
2022-08-09 06:22:00 【考拉盖饭】
单例模式
单例模式分饿汉式和懒汉式
饿汉式:
在类加载时就完成了初始化,所以类加载比较慢,不过这个慢也是看实例对象的大小,一般的单例都不会存放大容量的内容,所以大部分在毫秒级别,但获取对象的速度快。
public class Hungry {
private Hungry(){
// 私有化构造器
}
private static Hungry HUNGRY = new Hungry(); // 饿汉式
// 对外提供获取对象的方法
public static Hungry getInstance() {
return HUNGRY;
}
}
懒汉式:
在类加载时不初始化,等到第一次被使用时才初始化,所以会有并发的可能存在。
1.双重检查懒汉式
public class Lazy {
private Lazy(){
// 私有化构造器
}
private volatile static Lazy lazy;
// 双重检查懒汉式,当然,这种方式可以被反射破坏,不过正常使用这一种就可以
public static Lazy getInstance() {
if (lazy==null){
synchronized (Lazy.class){
if(lazy==null){
lazy = new Lazy();
}
}
}
return lazy;
}
public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException {
Lazy instance = Lazy.getInstance();
Constructor<Lazy> declaredConstructor = Lazy.class.getDeclaredConstructor(new Class[]{
});
declaredConstructor.setAccessible(true);
Lazy lazy = declaredConstructor.newInstance();
System.out.println(instance);
System.out.println(lazy);
/*for (int i = 1; i <= 10; i++) { new Thread(()->{ Lazy.getInstance(); },String.valueOf(i)).start(); }*/
}
}
2.枚举懒汉式
// 因为枚举本身自动支持序列化机制,绝对防止多次实例化
public enum EnumSingle {
SINGLE;
public static EnumSingle getInstance(){
return SINGLE;
}
}
// 试图通过反射来破坏枚举的单例也是不成功的
class Test{
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
EnumSingle instance = EnumSingle.getInstance();
Constructor<EnumSingle> declaredConstructor = EnumSingle.class.getDeclaredConstructor(String.class,int.class);
declaredConstructor.setAccessible(true);
EnumSingle enumSingle = declaredConstructor.newInstance();
System.out.println(instance);
System.out.println(enumSingle);
}
}
边栏推荐
- [MySQL] Second, the relationship between processes, MySQL password cracking, table building and database building related commands
- 【Feel】Unity Feel插件中,Camera无法正确显示CameraShake
- .NET高级技术
- [R language] Extract all files under a folder to a specific folder
- VScode安装了ESlint以后不管用、不生效的解决办法
- 【Feel】In the Unity Feel plugin, Camera cannot display CameraShake correctly
- 数据中台项目前期总结
- Redis 2 - 高级
- 运算放大器(OPA)超详细参数讲解-运放---以及8个型号的运算放大器分析对比
- Remember a nest.js route that matches all the path problems that follow
猜你喜欢
sql problem solving statement to create a table
Magnetic Core-Shell Fe3O4 Particles Supported Gold Nanostars | Magnetic Fe3O4-POSS-COOH | Superparamagnetic Fe3O4-Polydopamine Core-Shell Nanoparticles
Unity C# 委托——事件,Action,Func的作用和区别
治疗消化性溃疡—Toronto Research Chemicals 甘氨酸铝
Cysteine/Galactose/Perylenediimide Functionalized Fe3O4 Fe3O4 Nanomaterials | Scientific Research Reagents
Harbor Enterprise Mirror Warehouse Construction
DevNet: Deviation Aware Networkfor Lane Detection
MYSQL高级篇-----查询截取分析,锁机制,主从复制
PDF不能打印和复制的问题如何解决?
GNNExplainer applied to node classification task
随机推荐
GNNExplainer applied to node classification task
【R语言】对文件进行归一化整理到各文件类型文件夹
普罗米修斯原理及节点发布
VScode安装了ESlint以后不管用、不生效的解决办法
Used to import the data type
PDF不能打印和复制的问题如何解决?
S7-200SMART PLC Modbus TCP通信
Superparamagnetic iron [email protected]@cadmium sulfide nanocore-shell structure material|Fe3O4 magnetic nanop
抗菌药物丨Toronto Research Chemicals 天冬酰胺D
剑指offer专项突击版第24天
代码目录结构
Gao Zelong, a famous digital collection expert and founder of the Digital Collection Conference, was interviewed by China Entrepreneur Magazine
kubernetes 安全
直播源码开发,点击扫描跳转到扫码页面
Web APIs BOM- 操作浏览器:本地存储
golang xml 处理动态属性
多行字符串排序在线工具
DevNet: Deviation Aware Networkfor Lane Detection
SiO2-NH2/Fe3O4表面氨基化的Fe3O4/SiO2复合磁性纳米粒子(齐岳试剂)
Excel受保护的工作表怎么操作?