java细节之单例synchronized

来源:互联网 发布:输入网络凭据是什么 编辑:程序博客网 时间:2024/06/05 20:45
线程安全的懒汉式单例类的实现:
public class Singleton {
    private static Singleton instance;
    private final static Object syncLock = new Object();
   
    private Singleton() {
       
    }
   
    public static Singleton getInstance(){
        if (instance == null) {
            synchronized (syncLock) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
       
        return instance;
    }
}
0 0
原创粉丝点击