Authentic Lazy Singleton Design Pattern

一、懒汉式单例设计模式1:
class Singleton {
private static Singleton instance;  

private Singleton() {
System.out.println("*********构造*************");
}

public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}

public void print() {
System.out.println(Thread.currentThread().getName() + " = Test Singleton!");
}
}

public class TestSingle {
public static void main(String[] args) {
new Thread(() -> {
Singleton.getInstance().print();
}).start();
new Thread(() -> {
Singleton.getInstance().print();
}).start();
new Thread(() -> {
Singleton.getInstance().print();
}).start();
new Thread(() -> {
Singleton.getInstance().print();
}).start();
new Thread(() -> {
Singleton.getInstance().print();
}).start();
new Thread(() -> {
Singleton.getInstance().print();
}).start();
new Thread(() -> {
Singleton.getInstance().print();
}).start();
}
}
After running the program The output is as follows:
*********Construction*************
*********Construction*************
Thread-1 = Test Singleton!
*********Construction *************
*********Construction *************
Thread-5 = Test Singleton!
*********Construction********** ***
Thread-3 = Test Singleton!
Thread-2 = Test Singleton!
Thread-4 = Test Singleton!
Thread-0 = Test Singleton!
Thread-6 = Test Singleton!
Analysis found; this class is not just instantiated An object, which goes against the concept of the singleton design pattern. Check the relevant information and find that: when the attribute of the class is not modified with the volatitle keyword, in a multi-threaded environment, each thread operates a copy of the attribute. When a thread changes the value of the attribute, It takes time (that is, there is a delay) after the value of the property is changed synchronously, so there is a situation where the singleton class instantiates several objects. The example diagram of the thread operation copy is as follows:


When the attribute is modified with volatile, once the attribute is set, the synchronization of the original variable will be automatically performed immediately without any delay.

Second , the modified authentic lazy singleton design pattern As follows:
class Singleton {
private volatile static Singleton instance; //After volatile modifies the property, it means that once the property is set, it will automatically synchronize the original variable immediately

private Singleton() { //Since it is a singleton, the constructor must privatization
System.out.println("**********constructs***************");
}

public static Singleton getInstance() {
if (instance == null ) {
synchronized (Singleton.class) { //Only this part of the operation is synchronized
if (instance == null) { //After each thread comes in, check whether the instance is null
instance = new Singleton();
}
}
}
return instance;
}

public void print() {
System.out.println(Thread.currentThread().getName());
}
}

public class TestSingle {
public static void main(String[] args) {
new Thread(() -> {
Singleton.getInstance().print();
}).start();
new Thread(() -> {
Singleton.getInstance().print();
}).start();
new Thread(() -> {
Singleton.getInstance().print();
}).start();
new Thread(() -> {
Singleton.getInstance().print();
}).start();
new Thread(() -> {
Singleton.getInstance().print();
}).start();
new Thread(() -> {
Singleton.getInstance().print();
}).start();
new Thread(() -> {
Singleton.getInstance().print();
}).start();
}
}
运行程序后输出结果如下:
**********构造***************
Thread-0
Thread-1
Thread-3
Thread-2
Thread-4
Thread-5
Thread-6
has written the authentic lazy singleton design pattern so far.
3. singleton design pattern
class Singleton {
private static Singleton instance = new Singleton(); //Directly instantiate the object

private Singleton( ) {}

public static Singleton getInstance() {
return instance;
}

public void print() {
System.out.println(Thread.currentThread().getName());
}
}

public class TestSingleton {
public static void main(String[ ] args) {
new Thread(() -> {
Singleton.getInstance().print();
}).start();
new Thread(() -> {
Singleton.getInstance().print();
}). start();
new Thread(() -> {
Singleton.getInstance().print();
}).start();
new Thread(() -> {
Singleton.getInstance().print();
}).start();
new Thread(() -> {
Singleton.getInstance().print();
}).start();
new Thread(() -> {
Singleton.getInstance().print();
}).start();
new Thread(() -> {
Singleton.getInstance().print();
}).start();
}
}
开发中单例的概念应用比较多

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327040164&siteId=291194637