当前位置:网站首页>The singleton pattern
The singleton pattern
2022-08-09 06:27:00 【Koala Rice Bowl】
单例模式
The singleton mode is divided into hungry and lazy
饿汉式:
在类加载时就完成了初始化,所以类加载比较慢,However, this slowness also depends on the size of the instance object,General singleton will not store large-capacity content,So mostly at the millisecond level,但获取对象的速度快.
public class Hungry {
private Hungry(){
// 私有化构造器
}
private static Hungry HUNGRY = new Hungry(); // 饿汉式
// 对外提供获取对象的方法
public static Hungry getInstance() {
return HUNGRY;
}
}
懒汉式:
在类加载时不初始化,等到第一次被使用时才初始化,So there may be concurrency.
1.双重检查懒汉式
public class Lazy {
private Lazy(){
// 私有化构造器
}
private volatile static Lazy lazy;
// 双重检查懒汉式,当然,This way can be broken by reflection,But you can use this one normally
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.enumerate lazy
// Because the enum itself automatically supports the serialization mechanism,绝对防止多次实例化
public enum EnumSingle {
SINGLE;
public static EnumSingle getInstance(){
return SINGLE;
}
}
// Attempts to destroy the enum's singleton via reflection were also unsuccessful
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);
}
}
边栏推荐
- 缓存技术使用
- 一道很简答但是没答对的SQL题
- redis 运行lua 脚本 出现Invalid argument(s)
- [R language] Normalize and organize files into folders of various file types
- Silently start over, the first page is also a new page
- Introduction to AIOT
- Introduction of convenient functions and convenient shortcut keys of vs tomato assistant
- 思维方法 解决问题的能力
- 中英文说明书丨TRC 交替醇(Catalogue NumberA575760)
- How to automatically fill down an excel table without dragging the mouse down
猜你喜欢
运放-运算放大器经典应用电路大全-应用电路大全
Excel受保护的工作表怎么操作?
安装flask
网络学习总结
redis 运行lua 脚本 出现Invalid argument(s)
Introduction of convenient functions and convenient shortcut keys of vs tomato assistant
Unity Gobang Game Design and Simple AI (2)
phpstudy 安装 flarum论坛
【Feel】Unity Feel插件中,Camera无法正确显示CameraShake
报错jinja2.exceptions.UndefinedError: ‘form‘ is undefined
随机推荐
超顺磁四氧化三铁@二氧化硅@硫化镉纳米核壳结构材料|表面接枝mPEG的Fe3O4磁性纳米颗粒(f-Fe3O4)|相关产品
CMake中INSTALL_RPATH与BUILD_RPATH问题
[GO]、数组与切片
Kubernetes apparmor profile
crc计算
idea中PlantUML插件使用
Text String Length Sorting - Online Tool
zip压缩包密码解密
C语言实现顺序栈和链队列
install flask
ZIP压缩包文件删除密码的方法
.NET高级技术
VS2019常用快捷键
[MySQL]二、进程的关系、MySQL密码破解、建表和建库相关命令
线程的6种状态
Can Jincang Database Set Transaction Automatic Commit?
报错:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disab
JMeter test - JMeter 】 【 upload multiple images/batch CSV file upload pictures interface parametric method
中英文说明书丨CalBioreagents 山羊抗人白蛋白,IgG组分
DDD 领域驱动设计