dubbo 版本 2.6.2
/**
* 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来避免这些类的重复创建:
cachedInstances.putIfAbsent(name, new Holder<Object>());