1. What is the singleton pattern
Scratch the concept of a single example model directly from the Internet. The Singleton Pattern is one of the simplest design patterns in Java. This type of design pattern is the creation pattern, which provides the best way to create objects.
This pattern involves a single class that is responsible for creating its own objects while ensuring that only a single object is created. This class provides a way to access its unique objects directly, without instantiating the objects of the class.
The singleton pattern has three characteristics:
1) A singleton class can have only one instance.
2) The singleton class must create its own unique instance.
3) The singleton class must provide this instance to all other objects.
Writing the singleton pattern
2.1 LanHanShi
Public class SingletonExample1 {// Private constructor private SingletonExample1() {} // singleton object private static SingletonExample1 instance = null; Public static SingletonExample1 getInstance() {if (instance == null) {instance = new SingletonExample1(); } return instance; }}Copy the code
2. The hungry
Public class SingletonExample2 {// Private constructor private SingletonExample2() {} // singleton private static SingletonExample2 instance = new SingletonExample2(); Public static SingletonExample2 getInstance() {return instance; }}Copy the code
3. Synchronized
Public class SingletonExample3 {// Private constructor private SingletonExample3() {} // singleton object private static SingletonExample3 instance = null; Public static synchronized SingletonExample3 getInstance() {if (instance == null) {instance = new SingletonExample3(); } return instance; }}Copy the code
4. Slacker style (double lock)
Public class SingletonExample4 {// Private constructor private SingletonExample4() {} // 1, memory = allocate(); // 2, instance = memory = allocate(); // 3, instance = memory = allocate(); // Memory = allocate() allocates memory for an object. Instance = memory sets instance to the newly allocated memory. CtorInstance () initializes an object private static SingletonExample4 instance = null; Public static SingletonExample4 getInstance() {if (instance == null) {// Synchronized (Singletonexample4.class) {// Synchronize lock if (instance == null) {instance = new SingletonExample4(); // A - 3 } } } return instance; }}Copy the code
5. Lazy (Double sync lock)
Thread B sees that instance is not null and returns the instance that has not been initialized
Code:
Public class SingletonExample4 {private SingletonExample4() {} public class SingletonExample4 {private SingletonExample4() {} //1. Memory =allocate() Instance = nemory Set instance to point to newly allocated memory // JVM and CPU optimizations occurred instruction reordering // singleton private static SingletonExample4 instance = null; Public static SingletonExample4 getInstance() {if (instance == null) {synchronized (SingletonExample4.class) { if (instance == null) { instance = new SingletonExample4(); // thread A executes here}}} return instance; }}Copy the code
The correct code to use volatile to disallow instruction reordering:
Public class SingletonExample4 {private SingletonExample4() {} public class SingletonExample4 {private SingletonExample4() {} //1. Memory =allocate() Instance = nemory Set instance to just allocated memory // JVM and CPU optimization instruction reordering occurred // singleton private static Volatile SingletonExample4 instance = null; Public static SingletonExample4 getInstance() {if (instance == null) {synchronized (SingletonExample4.class) { if (instance == null) { instance = new SingletonExample4(); // thread A executes here}}} return instance; }}Copy the code
6. Hungry (static code block)
In static block write singleton mode, it is important to note that the object declaration must precede the static block assignment (note the order in which the code is written), otherwise the value obtained will be null
Public class SingletonExample6 {// Private constructor private SingletonExample6() {} // singleton private static SingletonExample6 instance = null; static { instance = new SingletonExample6(); } public static SingletonExample6 getInstance() {return instance; } public static void main(String[] args) { System.out.println(getInstance().hashCode()); System.out.println(getInstance().hashCode()); }}Copy the code
7. Lazy (enumerated)- Recommended
In the recommended way, the JVM guarantees that this method is called absolutely once and is called once
/** * Enumeration mode: Safest */ public SingletonExample7 {// Private constructor private SingletonExample7() {} public static SingletonExample7 getInstance() { return Singleton.INSTANCE.getInstance(); } private enum Singleton { INSTANCE; private SingletonExample7 singleton; Singleton() {Singleton = new SingletonExample7(); Singleton = new SingletonExample7(); } public SingletonExample7 getInstance() { return singleton; }}}Copy the code
Reference Documents:
The singleton pattern