Java: Thread-safe implementation of Singleton mode

Thread-safe implementation code

public class Singleton {
    
      

    private volatile static Singleton singleton;  
    
    private Singleton (){
    
    }  
    
    public static Singleton getSingleton() {
    
      
        if (singleton == null) {
    
      
            synchronized (Singleton.class) {
    
      
                if (singleton == null) {
    
      
                    singleton = new Singleton();  
                }  
            }  
        }  
        return singleton;  
    }  
}

Guess you like

Origin blog.csdn.net/mouday/article/details/132897540