当前位置:网站首页>Singleton singleton mode
Singleton singleton mode
2022-06-28 06:25:00 【Daily enlightenment】
(1) The first one is
Hungry Chinese style : After class is loaded into memory , Just instantiate a single instance ,JVM Ensure thread safety , Simple and practical , Recommended ! The only drawback : Whether it's used or not , Class is instantiated when it is loaded ( When not in use , Why are you loading it ).
final Must be initialized .
public class Mgr01 {
private static final Mgr01 INSTANCE = new Mgr01();
private Mgr01() {};
public static Mgr01 getInstance() {
return INSTANCE;
}
public void m() {
System.out.println("m");
}
}
Test code :
Because the construction method is private Of , Only through .getInstance() Method get object .
public static void main(String[] args) {
Mgr01 m1 = Mgr01.getInstance();
Mgr01 m2 = Mgr01.getInstance();
System.out.println(m1 == m2);
}
(2) The second kind
It has the same meaning as the first method
public class Mgr02 {
private static final Mgr02 INSTANCE;
static {
INSTANCE = new Mgr02();
}
private Mgr02() {};
public static Mgr02 getInstance() {
return INSTANCE;
}
public void m() {
System.out.println("m");
}
}
Test code :
public static void main(String[] args) {
Mgr02 m1 = Mgr02.getInstance();
Mgr02 m2 = Mgr02.getInstance();
System.out.println(m1 == m2);
}
(3) The third kind of
lazy loading It's also called the sluggard style , Although it achieves the goal of on-demand initialization , But it brings thread insecurity .
public class Mgr03 {
private static Mgr03 INSTANCE;
private Mgr03() {
}
public static Mgr03 getInstance() {
if (INSTANCE == null) {
INSTANCE = new Mgr03();
}
return INSTANCE;
}
public void m() {
System.out.println("m");
}
}
Test code :
public class Mgr03 {
private static Mgr03 INSTANCE;
private Mgr03() {
}
public static Mgr03 getInstance() {
if (INSTANCE == null) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Mgr03();
}
return INSTANCE;
}
public void m() {
System.out.println("m");
}
public static void main(String[] args) {
for(int i=0; i<100; i++) {
new Thread(()->
System.out.println(Mgr03.getInstance().hashCode())
).start();
}
}
}
(4) A fourth
lazy loading It's also called the sluggard style , Although it achieves the goal of on-demand initialization , But it brings thread insecurity , Can pass synchronized solve , But it also leads to a decline in efficiency .
public class Mgr04 {
private static Mgr04 INSTANCE;
private Mgr04() {
}
public static synchronized Mgr04 getInstance() {
if (INSTANCE == null) {
INSTANCE = new Mgr04();
}
return INSTANCE;
}
public void m() {
System.out.println("m");
}
}
Test code :
public class Mgr04 {
private static Mgr04 INSTANCE;
private Mgr04() {
}
public static synchronized Mgr04 getInstance() {
if (INSTANCE == null) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Mgr04();
}
return INSTANCE;
}
public void m() {
System.out.println("m");
}
public static void main(String[] args) {
for(int i=0; i<100; i++) {
new Thread(()->{
System.out.println(Mgr04.getInstance().hashCode());
}).start();
}
}
}
(5) The fifth
lazy loading It's also called the sluggard style , Although it achieves the goal of on-demand initialization , But it brings thread insecurity , Can pass synchronized solve , But it also leads to a decline in efficiency .
public class Mgr05 {
private static Mgr05 INSTANCE;
private Mgr05() {
}
public static Mgr05 getInstance() {
if (INSTANCE == null) {
// Trying to improve efficiency by reducing synchronous code blocks , And then it doesn't work
synchronized (Mgr05.class) {
INSTANCE = new Mgr05();
}
}
return INSTANCE;
}
public void m() {
System.out.println("m");
}
}
Test code :
public class Mgr05 {
private static Mgr05 INSTANCE;
private Mgr05() {
}
public static Mgr05 getInstance() {
if (INSTANCE == null) {
// Trying to improve efficiency by reducing synchronous code blocks , And then it doesn't work
synchronized (Mgr05.class) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Mgr05();
}
}
return INSTANCE;
}
public void m() {
System.out.println("m");
}
public static void main(String[] args) {
for(int i=0; i<100; i++) {
new Thread(()->{
System.out.println(Mgr05.getInstance().hashCode());
}).start();
}
}
}
(6) 6 kinds of
lazy loading, It's also called the sluggard style , Although it achieves the goal of on-demand initialization , But it brings thread insecurity .
public class Mgr06 {
private static volatile Mgr06 INSTANCE; //JIT
private Mgr06() {
}
public static Mgr06 getInstance() {
if (INSTANCE == null) {
// Double check
synchronized (Mgr06.class) {
if(INSTANCE == null) {
INSTANCE = new Mgr06();
}
}
}
return INSTANCE;
}
public void m() {
System.out.println("m");
}
}
Test code :
public class Mgr06 {
private static volatile Mgr06 INSTANCE; //JIT
private Mgr06() {
}
public static Mgr06 getInstance() {
if (INSTANCE == null) {
// Double check
synchronized (Mgr06.class) {
if(INSTANCE == null) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Mgr06();
}
}
}
return INSTANCE;
}
public void m() {
System.out.println("m");
}
public static void main(String[] args) {
for(int i=0; i<100; i++) {
new Thread(()->{
System.out.println(Mgr06.getInstance().hashCode());
}).start();
}
}
}
(7) Seventh kinds
Static inner class mode ,JVM A guarantee , When loading external classes, internal classes are not loaded , This allows lazy loading .
public class Mgr07 {
private Mgr07() {
}
private static class Mgr07Holder {
private final static Mgr07 INSTANCE = new Mgr07();
}
public static Mgr07 getInstance() {
return Mgr07Holder.INSTANCE;
}
public void m() {
System.out.println("m");
}
}
Test code :
public static void main(String[] args) {
for(int i=0; i<100; i++) {
new Thread(()->{
System.out.println(Mgr07.getInstance().hashCode());
}).start();
}
}
(8) Eighth
It can not only solve the problem of thread synchronization , It also prevents deserialization ( Perfect ).
public enum Mgr08 {
INSTANCE;
public void m() {}
}
Test code :
public static void main(String[] args) {
for(int i=0; i<100; i++) {
new Thread(()->{
System.out.println(Mgr08.INSTANCE.hashCode());
}).start();
}
}
边栏推荐
- Example of MVVM framework based on kotlin+jetpack
- lombok @EqualsAndHashCode 注解如何让对象.equals()方法只比较部分属性
- 【Paper Reading-3D Detection】Fully Convolutional One-Stage 3D Object Detection on LiDAR Range Images
- @The reason why the Autowired annotation is empty
- Oracle fundamentals summary
- High quality domestic stereo codec cjc8988, pin to pin replaces wm8988
- Select trigger event from easyUI drop-down box
- Students who do not understand the code can also send their own token. The current universal dividend model can be divided into BSC and any generation B
- Create a gson object that formats the time zone. JSON parsing time formatting zoneddatetime
- Idea generates entity classes from database tables
猜你喜欢
eyebeam高级设置
移动广告发展动向:撬动存量,精细营销
What are the advantages of e-mail marketing? Why do sellers of shopline independent station attach so much importance to it?
CAD二次开发+NetTopologySuite+PGIS 引用多版本DLL问题
Lombok @equalsandhashcode annotation how to make objects The equals () method compares only some attributes
ROS rviz_satellite功能包可视化GNSS轨迹,卫星地图的使用
Linux MySQL implements root user login without password
Xcode13.3.1 error reported after pod install
Freeswitch使用originate转dialplan
Alert pop-up processing in Web Automation
随机推荐
YYGH-BUG-02
Select trigger event from easyUI drop-down box
freeswitch设置最大呼叫时长
Shell script one click deployment (MySQL)
Scripting and programming languages
图片按日期批量导入WPS表格
Interpretation of Blog
eyebeam高级设置
报错--解决core-js/modules/es.error.cause.js报错
Yolact++ pytoch environment
AutoCAD C polyline self intersection detection
Yygh-7-user management
Yolov5 adds a small target detection layer
idea根据数据库表生成实体类
Error reporting - resolve core JS / modules / es error. cause. JS error
Eyebeam advanced settings
YYGH-BUG-03
整型提昇和大小端字節序
Tryout title code
Uni app wechat applet sharing function