What is the singleton pattern
A class with only one instance is called a singleton. This design pattern is also known as the singleton pattern. Another way of saying this is that the singleton pattern creates one and only one instance in the current process.
Why use singleton mode instead of static method?
Both actually serve our ultimate purpose of loading, but they are object based and object oriented. Just as we can solve problems without object orientation, object oriented code provides a better programming idea.
So we use the singleton pattern, or a static method to maintain a these values has one and only this one, but at the moment these configurations and properties is obtained by object oriented coding way, we should use the singleton pattern, or is not object-oriented, but he attributes should be in the face of the object itself, we use a static method can also solve the problem, But the best solution would also be to use the singleton pattern.
In addition, the singleton mode supports lazy loading to avoid resource waste.
What are the forms of singleton mode
1) Hungry
When the class is loaded, the instance is initialized. The JVM guarantees uniqueness and security.
Note: The JVM is designed to ensure that when a class is loaded, it is thread exclusive
2) Slacker
Lazy supports lazy loading, but be aware of thread safety issues.
Note: there is some insecurity, if used, try to use double lock delay loading.
Objects should be volatile, otherwise reordering problems will occur.
3) Static inner class
Similar to hungry, but can achieve lazy loading. The JVM guarantees uniqueness and security.
Note: The JVM is designed to ensure that when a class is loaded, it is thread exclusive
4) enumerate singletons
It’s like hungry
The writing method is concise, the code is short and pithy.
Thread safety.
Prevent deserialization and reflection from breaking.
Why is enumeration the best implementation of the singleton pattern
Because enumerations are written succinctly, they can be thread safe and corrupt.
Five, the implementation of multiple examples
The multi-example pattern is similar to the factory pattern in that the multi-example pattern creates objects of the same class, whereas the factory pattern creates objects of different subclasses
What is instruction reordering
Instruction reordering refers to the multi-level cache storage defined by the JIT compiler, CPU processor, and JMM that optimizes the order in which original statements are executed while compiling bytecode and running machine instructions without affecting the program’s final execution. JMM multilevel caching causes statements to be executed without necessarily following the correct read and write operations. But these are all actions that the JMM allows. Therefore, synchronization is required to prevent reordering of related instructions, such as memory barriers.
Example: Demo instance = new Demo(); Rather than an atomic operation, it does at least three things in the JVM:
Allocate memory on the heap for instance. (Allocate memory)
Call the constructor of DoubleCheckIdGenerator and so on to initialize instance. (Initialization)
Point the instance object to the allocated memory space. (Instance is not null after this step.)
In the absence of volatile modifiers, the order of execution can be 1,2,3, or 1,3,2. Let’s say there are two threads, and when one thread executes 3 before 2, the second thread goes to the first check, finds that instance is not null, and returns directly. This is a problem, and the instance is not fully initialized.