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>());
相關文章
相關標籤/搜索