The singleton patternThat is, only one object instance of a class exists in a JVM memory, and a global access point is provided to access it.

classification

There are, of course, other ways to generate singletons, such as double check locks, enumerations, and static inner classes

LanHanShi

  1. The thread is unsafe and unavailable
public class Singleton { 
     private static Singleton instance; 
     private Singleton (a){} 
  
     public static Singleton getInstance(a) { 
     if (instance == null) { 
         instance = new Singleton(); 
     } 
     returninstance; }}Copy the code
  1. Thread-safe, synchronous method, low efficiency, not recommended
public class Singleton { 
     private static Singleton instance; 
     private Singleton (a){} 
     public static synchronized Singleton getInstance(a) { 
     if (instance == null) { 
         instance = new Singleton(); 
     } 
     returninstance; }}Copy the code
  1. Threads are not safe, creating multiple instances and not available
public class Singleton {
     private static Singleton singleton;
     private Singleton(a) {}

     public static Singleton getInstance(a) {
         if (singleton == null) {
             synchronized (Singleton.class) {
                 singleton = newSingleton(); }}returnsingleton; }}Copy the code

The hungry type

There is no thread safety problem, cannot delay loading, affecting system performance 4.

public class Singleton { 
    private static Singleton instance = new Singleton(); 
    private Singleton (a){} 
    public static Singleton getInstance(a) { 
    	returninstance; }}Copy the code
public class Singleton { 
     private static Singleton instance = null; 
     static { 
 		instance = new Singleton(); 
     } 
     private Singleton (a){} 
     public static Singleton getInstance(a) { 
  		returninstance; }}Copy the code
  1. Double check lock is recommended for thread safety
public class Singleton {
    private static volatile Singleton singleton;

    private Singleton(a) {}public static Singleton getInstance(a) {
        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    singleton = newSingleton(); }}}returnsingleton; }}Copy the code
  1. Static internal class, thread-safe, active call only instantiation, lazy load efficiency, recommended
public class Singleton { 
    private static class SingletonHolder { 
  		private static final Singleton INSTANCE = new Singleton(); 
    } 
    private Singleton (a){} 
    public static final Singleton getInstance(a) { 
  		returnSingletonHolder.INSTANCE; }}Copy the code
  1. Enumerated types, with no thread-safety issues, avoid creating new instances of anti-sequence, rarely used
public enum Singleton { 
     INSTANCE; 
     public void whateverMethod(a) {}}Copy the code

Matters needing attention

Private Singleton() {} 3, if the class is serializable, consider deserialization to generate multiple instances of the problem, the solution is as follows

private Object readResolve(a) throws ObjectStreamException { 
  	// instead of the object we're on, return the class variable INSTANCE 
	return INSTANCE; 
}
Copy the code

Usage scenarios

1. Tool class object 2. A class that has only one instance in the system 3

Here’s how the singleton pattern works in the JDK In addition, instances in the Spring container are singleton types by default, which means that the container instantiates beans into the container at startup. You can also set the lazy defalut-lazy-init=”true” for lazy instantiation and then instantiate them.

eggs

Welcome to join the request group to learn exchange: Java/Python architect ① group: 415777345 PC users quickly join (click the hyperlink) :415777345Mobile QQ scan code to join:SpringBoot SpringCloud Docker K8s ① 类 : 317945902 PC users quickly join (click hyperlink) :317945902Mobile QQ scan code to join:Welcome to group exchange and discussion!