当前位置 : 主页 > 编程语言 > java >

实现一个简单的单例模式

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt public class Singleton { private Singleton() { } private volatile static Singleton instance = null; public static Singleton getInstance() { if(instance == null) { synchronized(this) { if(instance == null) { instance = new Sing
gistfile1.txt
public class Singleton {

    private Singleton() {

    }

    private volatile static Singleton instance = null;

    public static Singleton getInstance() {
        if(instance == null) {
            synchronized(this) {
                if(instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}
网友评论