构造函数线程安全-即dubbo的extensionLoader存在线程安全问题
来源:互联网 发布:linux查看当前路径文件 编辑:程序博客网 时间:2024/04/30 18:08
构造函数是静态的,而且不管是否为private还是public修饰,他都是静态的。
《JAVA并发编程实战》有提过:
“”如果构造方法暴露了this指针,就会存在线程安全问题“”
其意思是,构造方法是静态的,非线程安全的,如果在这个非线程安全的方法中读写对象的成员,比如初始化对象,就会存在线程安全问题。
比如:
class Holder {
private int n;
public Holder( int n){
this.n = n;
}
}
在dubbo的ExtensionLoader也存在这个问题:
@SuppressWarnings("unchecked")public static <T> ExtensionLoader<T> getExtensionLoader(Class<T> type) { if (type == null) throw new IllegalArgumentException("Extension type == null"); if(!type.isInterface()) { throw new IllegalArgumentException("Extension type(" + type + ") is not interface!"); } if(!withExtensionAnnotation(type)) { throw new IllegalArgumentException("Extension type(" + type + ") is not extension, because WITHOUT @" + SPI.class.getSimpleName() + " Annotation!"); } ExtensionLoader<T> loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type); if (loader == null) { EXTENSION_LOADERS.putIfAbsent(type, new ExtensionLoader<T>(type)); loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type); } return loader;}private ExtensionLoader(Class<?> type) { this.type = type; objectFactory = (type == ExtensionFactory.class ? null : ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension());}
下面的构造方法做了非线程安全的赋值操作
这个extensionLoader的是单例缓存的,在构造的时候没有同步,直接被用来初始化.
所以存在线程安全。
造成的结果:获取扩展类的时候,可能会失败,但这个问题不会影响dubbo的正常时候,只会占用内存
0 0
- 构造函数线程安全-即dubbo的extensionLoader存在线程安全问题
- hashMap非线程安全问题和concurrenthashmap线程安全的比较
- Java线程安全问题及线程安全解决方法
- 从构造函数看线程安全
- 从构造函数看线程安全
- SimpleDateFormat时间格式化存在线程安全问题
- SimpleDateFormat时间格式化存在线程安全问题
- 关于SimpleDateFormat安全的时间格式化线程安全问题
- 关于SimpleDateFormat安全的时间格式化线程安全问题
- 关于SimpleDateFormat安全的时间格式化线程安全问题
- 关于SimpleDateFormat安全的时间格式化线程安全问题
- 关于SimpleDateFormat安全的时间格式化线程安全问题
- 关于SimpleDateFormat安全的时间格式化线程安全问题
- 关于SimpleDateFormat安全的时间格式化线程安全问题
- 关于SimpleDateFormat安全的时间格式化线程安全问题
- 关于SimpleDateFormat安全的时间格式化线程安全问题
- 关于SimpleDateFormat安全的时间格式化线程安全问题
- GTK+的线程安全问题
- 32位系统和64位区别
- Unity3D:动态加载(AssetBundle)
- cocoapods试验版
- qt出现 undefined reference to pcap_xxx
- redis基础简介(五)- 数据备份与恢复、数据安全、性能测试、客户端连接、分区
- 构造函数线程安全-即dubbo的extensionLoader存在线程安全问题
- This version of Android Studio is incompatible with the Gradle Plugin used.
- Hive动态分区
- C语言之变量类型和存储方式
- Spring cache 配置代理 改为aspectj
- 新年新气象,新的一年新的开始,给自己定个小小的目标,以此为证
- spring-boot配置文件-security(中文翻译)
- JS中的this指向相关
- 自定义控件实践——单选/多选文本标签流