当前位置:网站首页>单例模式
单例模式
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);
}
}
边栏推荐
- mongo+ycsb性能测试及线程数分析
- sqlserver导入数据类型问题
- IQ Products巨细胞病毒CMV感染检测试剂盒的特征和应用
- 力扣刷题180
- Kubernetes apparmor profile
- 【Feel】In the Unity Feel plugin, Camera cannot display CameraShake correctly
- kubernetes apparmor 简介
- Remember a nest.js route that matches all the path problems that follow
- Excel受保护的工作表怎么操作?
- 【Feel】Unity Feel插件中,Camera无法正确显示CameraShake
猜你喜欢

CalBioreagents超全Id 蛋白兔单克隆抗体,助力科研

抗菌药物丨Toronto Research Chemicals 天冬酰胺D

推进产教融合 赋能教育创新发展 | 华云数据荣获“企业贡献奖”

治疗消化性溃疡—Toronto Research Chemicals 甘氨酸铝

redis 运行lua 脚本 出现Invalid argument(s)

pdf加密、找回密码

5年测试开发工程师感悟——写给还在迷茫中的朋友

SiO2/KH550修饰四氧化三铁纳米磁性颗粒|PDA包裹四氧化三铁磁性纳米颗粒(科研级)

workbench 数据导出

A test engineer with an annual salary of 35W was laid off. Personal experience: advice that you have to listen to
随机推荐
【R语言】交互作用 测试数据
【R语言】对文件进行归一化整理到各文件类型文件夹
sql problem solving statement to create a table
VB.net程序关闭后后台还在与SQL连接
像天才一样思考:如何培养自己的创造力?
SiO2/KH550修饰四氧化三铁纳米磁性颗粒|PDA包裹四氧化三铁磁性纳米颗粒(科研级)
锁执行的过程
简单使用Lambda表达式
手把手教你用C语言制作七夕流星雨---优雅永不过时(详细教程)
治疗消化性溃疡—Toronto Research Chemicals 甘氨酸铝
MYSQL高级篇-----查询截取分析,锁机制,主从复制
[GO]、数组与切片
redis 运行lua 脚本 出现Invalid argument(s)
sqlserver导入数据类型问题
超顺磁四氧化三铁@二氧化硅@硫化镉纳米核壳结构材料|表面接枝mPEG的Fe3O4磁性纳米颗粒(f-Fe3O4)|相关产品
mongo+ycsb性能测试及线程数分析
ZIP压缩包文件删除密码的方法
uniapp实现防抖搜索
kubernetes security
Harbor Enterprise Mirror Warehouse Construction