dubbo 扩展单例的保存

dubbo 版本 2.6.2java

/**
     * Find the extension with the given name. If the specified name is not found, then {[@link](https://my.oschina.net/u/393) IllegalStateException}
     * will be thrown.
     */
    @SuppressWarnings("unchecked")
    public T getExtension(String name) {
        if (name == null || name.length() == 0)
            throw new IllegalArgumentException("Extension name == null");
        if ("true".equals(name)) {
            return getDefaultExtension();
        }
        Holder<Object> holder = cachedInstances.get(name);
        if (holder == null) {
            cachedInstances.putIfAbsent(name, new Holder<Object>());
            holder = cachedInstances.get(name);
        }
        Object instance = holder.get();
        if (instance == null) {
            synchronized (holder) {
                instance = holder.get();
                if (instance == null) {
                    instance = createExtension(name);
                    holder.set(instance);
                }
            }
        }
        return (T) instance;
    }

因为真实的扩展类多是比较重量级的类,dubbo使用一个轻量级的Holder来避免这些类的重复建立:.net

cachedInstances.putIfAbsent(name, new Holder<Object>());
相关文章
相关标签/搜索