我已经阅读了很多,但还没有找到明确的答案。
我有一个看起来像这样的类:
public class Foo() {
private static final HashMap<String, HashMap> sharedData;
private final HashMap myRefOfInnerHashMap;
static {
// time-consuming initialization of sharedData
final HashMap<String, String> innerMap = new HashMap<String, String>;
innerMap.put...
innerMap.put...
...a
sharedData.put(someKey, java.util.Collections.unmodifiableMap(innerMap));
}
public Foo(String key) {
this.myRefOfInnerHashMap = sharedData.get(key);
}
public void doSomethingUseful() {
// iterate over copy
for (Map.Entry<String, String> entry : this.myRefOfInnerHashMap.entrySet()) {
...
}
}
}
而且我想知道从 Foo 的实例访问 sharedData 是否是线程安全的(如构造函数和 doSomethingUseful() 中所示)。Foo 的许多实例将在多线程环境中创建。
我的意图是 sharedData 在静态初始化程序中初始化,之后不再修改(只读)。
我读到的是不可变对象本质上是线程安全的。但我只在似乎是实例变量的上下文中看到了这一点。不可变的静态变量是线程安全的吗?
我发现的另一个构造是 ConcurrentHashMap。我可以制作 ConcurrentHashMap 类型的 sharedData,但它包含的 HashMaps 也必须是 ConcurrentHashMap 类型吗?基本上..
private static final ConcurrentHashMap<String, HashMap> sharedData;
或者
private static final ConcurrentHashMap<String, ConcurrentHashMap> sharedData;
还是会更安全(但简单地克隆()成本更高)?
this.myCopyOfData = sharedData.get(key).clone();
TIA。
(静态初始化器已被编辑以提供更多上下文。)