当前位置:网站首页>Enumeration class implements singleton mode
Enumeration class implements singleton mode
2022-07-27 05:23:00 【Master yuan】
List of articles
1. The singleton pattern
I believe everyone is familiar with the singleton mode , Here is an example of using enumeration classes to implement the singleton pattern .
1.1 Hungry Han style single case mode
public class YYSingletonDemo {
private YYSingleton() {
}
private enum YYSingleton {
INSTANCE;
}
public static YYSingleton getInstance() {
return YYSingleton.INSTANCE;
}
}
1.2 Lazy singleton pattern
public class YYSingleton {
private YYSingleton() {
}
private enum YYSingletonHolder {
INSTANCE;
private YYSingleton instance = null;
private YYSingleton getYYSingleton() {
instance = new YYSingleton();
return instance;
}
}
public static YYSingleton getInstance() {
return YYSingletonHolder.INSTANCE.instance;
}
}
2. Why is using enum classes thread safe singleton mode ?
Due to the particularity of enumeration classes , Ensure the uniqueness of enumeration types . Because the enumeration class will be loaded at compile time , Therefore, it belongs to the starving Han style singleton mode . But if you treat it as a member variable , Only when in use will it be loaded , It belongs to lazy singleton mode .
边栏推荐
- [CSAPP] Application of bit vectors | encoding and byte ordering
- B1025 反转链表*******
- LeetCode之6 ZigZag Conversion
- B1026 程序运行时间
- 枚举类实现单例模式
- How to test the payment process?
- JVM上篇:内存与垃圾回收篇九--运行时数据区-对象的实例化,内存布局与访问定位
- 消息可靠性处理
- ERROR! MySQL is not running, but PID file exists
- MQ set expiration time, priority, dead letter queue, delay queue
猜你喜欢

JVM上篇:内存与垃圾回收篇七--运行时数据区-堆

Shell course summary

How to store the startprocessinstancebykey method in acticiti in the variable table

BIO、NIO、AIO区别

MQ set expiration time, priority, dead letter queue, delay queue

JVM Part 1: memory and garbage collection -- runtime data area 4 - program counter

Prime number screening (Ehrlich sieve method, interval sieve method, Euler sieve method)

Machine learning overview

How to sinicize the JMeter interface?

JVM Part 1: memory and garbage collection part 14 -- garbage collector
随机推荐
Machine learning overview
Explore the mysteries of the security, intelligence and performance of the universal altek platform!
2021 OWASP top 5: security configuration error
辗转相除法
[Niuke discussion area] Chapter 7: building safe and efficient enterprise services
素数筛选(埃氏筛法,区间筛法,欧拉筛法)
听过最自律的一句话: 那些我难以言表 不作声响
求组合数(最强优化)
Translation of robot and precise vehicle localization based on multi sensor fusion in diverse city scenes
JVM上篇:内存与垃圾回收篇十四--垃圾回收器
How to test the payment process?
Message reliability processing
B1028 人口普查
稀疏数组→五子棋的存盘续盘等操作
JVM Part 1: memory and garbage collection part 11 -- execution engine
B1024 科学计数法
BIO、NIO、AIO区别
Typescript details
Li Kou achieved the second largest result
torch中乘法整理,*&torch.mul()&torch.mv()&torch.mm()&torch.dot()&@&torch.mutmal()