你真的懂怎么写双重加锁的单例模式吗?

319 阅读1分钟

我发现掘金上很多人在写单例模式,掘金还每次“加精”,不是很看得懂掘金这种做法。

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

问问各位大大,这种写法对不对???