【发布时间】:2016-05-22 08:37:06
【问题描述】:
由于意外的属性值,我收到错误,我正在尝试缩小原因。该属性由以下示例类从文件中加载:
public final class PropertyLoader {
private enum Instances{
ELVIS;
private final PropertyLoader loader;
Instances() {
this.loader = new PropertyLoader();
}
}
private boolean isPropertyEnabled;
private PropertyLoader() {
loadProperties();
}
public static PropertyLoader getInstance() {
Instances.ELVIS.loader;
}
private void loadProperties() {
this.isPropertyEnabled = loadPropertyFromFile(FILE, "enabled");
//... more properties
}
public boolean isPropertyEnabled() {
// eventually returns unexpected value
return this.isPropertyEnabled;
}
}
这个实现是线程安全的吗?如果没有,如何在不更改接口的情况下改进实现?有没有一种有效的策略来测试这个类的并发问题?
【问题讨论】:
标签: java thread-safety singleton properties-file