【发布时间】:2011-08-03 20:24:31
【问题描述】:
嗨
下面的类是线程安全的吗?
class ImmutablePossiblyThreadsafeClass<K, V> {
private final Map<K, V> map;
public ImmutablePossiblyThreadsafeClass(final Map<K, V> map) {
this.map = new HashMap<K, V>();
for (Entry<K, V> entry : map.entrySet()) {
this.map.put(entry.getKey(), entry.getValue());
}
}
public V get(K key) {
return this.map.get(key);
}
}
【问题讨论】:
-
也许你应该使用
this.map.putAll(map)而不是那个循环。
标签: java concurrency thread-safety immutability