常用设计模式

2019-03-12  本文已影响0人  浩仔_Boy

1.单例模式:

class SimpleInstance {
    //volatile变量仍然有工作内存的拷贝,但是由于它特殊的操作顺序性规定,所以看起来如同直接在主内存中读写访问一般
    private static volatile SimpleInstance instance = null;

    public static SimpleInstance getInstance() {
        if (instance == null) {
            synchronized (SimpleInstance.class) {
                if (instance == null) {
                    instance = new SimpleInstance();
                }
            }
        }
        return instance;
    }
}

上一篇 下一篇

猜你喜欢

热点阅读