当前位置:网站首页>单例模式
单例模式
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);
}
}
边栏推荐
- 二十四节气之立秋
- GNNExplainer applied to node classification task
- 牛客每日刷题之链表
- ZIP压缩包文件删除密码的方法
- [HNOI2002]营业额统计
- Magnetic Core-Shell Fe3O4 Particles Supported Gold Nanostars | Magnetic Fe3O4-POSS-COOH | Superparamagnetic Fe3O4-Polydopamine Core-Shell Nanoparticles
- IQ Products CMV Brite Turbo试剂盒的原理
- 输入框最前面添加放大镜&&background-image和background-color冲突问题
- Molybdenum disulfide/hafnium dioxide composite nanomaterials (MoS2/HfO2) | tantalum-doped hafnium dioxide nanoparticles (Qi Yue bio)
- Fe3O4/SiO2 Composite Magnetic Nanoparticles Aminated on SiO2-NH2/Fe3O4 Surface (Qiyue Reagent)
猜你喜欢
sql问题解答创建表的语句
运算放大器(OPA)超详细参数讲解-运放---以及8个型号的运算放大器分析对比
手把手教你用C语言制作七夕流星雨---优雅永不过时(详细教程)
数据中台项目前期总结
运放-运算放大器经典应用电路大全-应用电路大全
C# 利用iTextSharp画PDF
锁执行的过程
Molybdenum disulfide/hafnium dioxide composite nanomaterials (MoS2/HfO2) | tantalum-doped hafnium dioxide nanoparticles (Qi Yue bio)
力扣刷题180
IQ Products CMV Brite Turbo试剂盒的原理
随机推荐
[GO], arrays and slices
Superparamagnetic iron [email protected]@cadmium sulfide nanocore-shell structure material|Fe3O4 magnetic nanop
Domain name batch query online tool
Kubernetes apparmor profile
S7-200SMART PLC Modbus TCP通信
[GO]、数组与切片
一道很简答但是没答对的SQL题
关于如何查找NXP S32K1xx系列单片机的封装信息和引脚定义
S7-200SMART PLC Modbus TCP communication
【R语言】把文件夹下的所有文件提取到特定文件夹
中英文说明书丨TRC D-阿卓糖(D-Altrose)
如何操作数据库
[MySQL] Second, the relationship between processes, MySQL password cracking, table building and database building related commands
Introduction to AIOT
mmdetection源码解析--ResNet18
Word文件的只读模式没有密码怎么退出?
为什么以太网无法接收大于1500字节的数据包?
金仓数据库能否设置事务自动提交
牛客每日刷题之链表
el-table缓存数据