《大话设计模式》java实现之单例模式

来源:互联网 发布:淘宝开网店要多少钱 编辑:程序博客网 时间:2024/05/16 04:59

单例模式是比较简单的,想起以前面试的时候也是遇到最多的,但是要真的写好里面也很多讲究,什么饥饿、线程安全等等,参考http://www.runoob.com/design-pattern/singleton-pattern.html这里只写双重锁定DCL的单例

public class Singleton {    private volatile static Singleton instance;    public Singleton() {        super();    }    public static Singleton getInstance() {        if(instance == null) {            synchronized(Singleton.class) {                if(instance == null) {                    instance = new Singleton();                }            }        }        return instance;    }}
原创粉丝点击