Prerequisites:
Objects that require frequent creation and destruction consume too much or too much resources to create.
Three elements:
- 1. Private construction methods;
- 2. instantiation of variable reference privatization;
- 3. There are a total of methods to obtain the instance.
1. Hunger examples
Disadvantage: instantiation is completed during class loading.
/*** Hungry Chinese Singleton *** @ author wonder * @ history create wonder 9:55:32, December 1, October 24, 2018 * @ version 1.0 */public class singleton1 {private singleton1 () {}// 1 privatize private final static singleton1 singleton1 = new singleton1 (); // 2 instantiate public static singleton1 getinstance () {// 3 provides return singleton1 ;}}
View code2. lazy Singleton
Disadvantage: Multiple Single objects are generated in a multi-threaded environment, and the thread is not secure.
/*** The lazy Singleton * generates multiple single objects in a multi-threaded environment ** @ author wonder * @ history create wonder 10:17:59, January 1, October 24, 2018 * @ version 1.0 */public class singleton2 {private singleton2 () {} // 1 privatized structure Private Static singleton2 singleton2 = NULL; // delayed instance public static singleton2 getinstance () {If (singleton2 = NULL) {singleton2 = new singleton2 () ;}return singleton2 ;}}
View code3. lazy Singleton (synchronized synchronization lock)
Disadvantages: Low Efficiency
Synchronous method: When obtaining an instance, the synchronization method is executed every time, which is too inefficient.
Synchronous Code block: multiple threads may enter the if Statement at the same time, and the actual page cannot be used for thread synchronization.
/*** The lazy Singleton locks only the part of the Code to be locked. ** @ author wonder * @ history create wonder 10:17:59, January 1, October 24, 2018 * @ version 1.0 */public class singleton4 {private singleton4 () {}// 1 privatized construction Private Static singleton4 single = NULL; // delayed instance public static singleton4 getinstance () {If (single = NULL) {synchronized (singleton4.class) {single = new singleton4 () ;}} return single ;}}
View code4. double check
1. Use the volatile keyword,Multi-thread visibility
Why volatile: https://www.cnblogs.com/damonhuang/p/5431866.html
2. Perform the IF (Singleton = NULL) check twice. If it is null, synchronize the code block and create an instance. Otherwise, the singleton instance is directly returned.
public class Singleton { private static volatile Singleton singleton; private Singleton() {} public static Singleton getInstance() { if (singleton == null) { synchronized (Singleton.class) { if (singleton == null) { singleton = new Singleton(); } } } return singleton; }}
View code5. static code block and static internal class
Static code block method
Public class singleton5 {private singleton5 () {}// 1 private structure Private Static singleton5 single = NULL; // delayed instance // static code block static {single = new singleton5 ();} public static singleton5 getinstance () {return single ;}}
View code
Better static internal class mode (lazy loading)
Static internal class mode is not immediately instantiated when the singleton class is loaded
public class Singleton { private Singleton() {} private static class SingletonInstance { private static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonInstance.INSTANCE; }}
View code6. enumeration implementation
Simple enumeration:
public enum Singleton { INSTANCE; public void whateverMethod() { }}
View code
Optimized: Internal enumeration class
Public class singletonfactory {// internal enumeration class private Enum enmusingleton {Singleton; private singleton6 single; // The enumeration class constructor is instantiated in class loading private enmusingleton () {single = new singleton6 ();} public singleton6 getinstance () {return single ;}} public static singleton6 getinstance () {return enmusingleton. singleton. getinstance () ;}} class singleton6 {public singleton6 (){}}
View code
Singleton mode (lazy, ELE. Me, Synchronous lock, static, and enumeration)