当前位置:网站首页>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();
}
}边栏推荐
- Linked list (II) - Design linked list
- AutoCAD C polyline small acute angle detection
- ROS rviz_satellite功能包可视化GNSS轨迹,卫星地图的使用
- Independent station sellers are using the five e-mail marketing skills, do you know?
- Freeswitch uses Mod_ Shot module plays mp3
- 【Paper Reading-3D Detection】Fully Convolutional One-Stage 3D Object Detection on LiDAR Range Images
- FPGA - 7 Series FPGA selectio -09- io of advanced logic resources_ FIFO
- 三极管驱动无刷电机
- How popular are FB and WhatsApp mass messages in 2022?
- death_ satan/hyperf-validate
猜你喜欢

链表(二)——设计链表

Linked list (III) - reverse linked list

JDBC learning (I) -- implementing simple CRUD operations

FPGA - 7 Series FPGA selectio -09- io of advanced logic resources_ FIFO

How popular are FB and WhatsApp mass messages in 2022?

Promotion intégrale et ordre des octets de fin de taille

YYGH-BUG-02

RN7302三相电量检测(基于STM32单片机)

Oracle condition, circular statement

报错--解决core-js/modules/es.error.cause.js报错
随机推荐
4. use MySQL shell to install and deploy Mgr clusters | explain Mgr in simple terms
Install and manage multiple versions of PHP under mac
Socket. Io long Connection Push, version Control, Real - Time Active user volume Statistics
Yolov5 adds a small target detection layer
OpenSCAP 简介
Teach you how to use UCOS
Error reporting - resolve core JS / modules / es error. cause. JS error
Iframe switching in Web Automation
Note that JPA uses a custom VO to receive jpql query results
链表(三)——反转链表
FPM tool installation
浮动与定位
Freeswitch uses Mod_ Shot module plays mp3
FPGA - 7 Series FPGA selectio -08- oserdese2 of advanced logic resources
Difficulty calculation of Ethereum classic
eyebeam高级设置
Promotion intégrale et ordre des octets de fin de taille
整型提升和大小端字节序
Introduction to openscap
Sharing tips for efficient scripting