多线程情况下的单例模式创建

来源:互联网 发布:python 生成安装程序 编辑:程序博客网 时间:2024/04/29 05:16

public class SingletonTest { 
 
    private static SingletonTest instance = null; 
 
    private SingletonTest() { 
    } 
 
    private static synchronized void syncInit() { 
        if (instance == null) { 
            instance = new SingletonTest(); 
        } 
    } 
 
    public static SingletonTest getInstance() { 
        if (instance == null) { 
            syncInit(); 
        } 
        return instance; 
    } 

0 0