Java 并发hashmap是否不需要同步的getter/setter?
如果我使用的是并发hashmap,并且我有设置和获取值的方法,就像我使用的是并发hashmap一样,那么我需要同步getter和setter吗?这是多余的吗?一种设计更好吗 另外,没有同步的并发hashmap是否比具有同步getter和setter的hashmap更快?这是一个高性能的系统 多谢各位Java 并发hashmap是否不需要同步的getter/setter?,java,hashmap,concurrenthashmap,Java,Hashmap,Concurrenthashmap,如果我使用的是并发hashmap,并且我有设置和获取值的方法,就像我使用的是并发hashmap一样,那么我需要同步getter和setter吗?这是多余的吗?一种设计更好吗 另外,没有同步的并发hashmap是否比具有同步getter和setter的hashmap更快?这是一个高性能的系统 多谢各位 java.util.concurrent.ConcurrentHashMap是线程安全的 它比使用synchronized(对象) 您仍然需要小心,不要通过这样的代码创建“逻辑”竞争条件 if (m
java.util.concurrent.ConcurrentHashMap
是线程安全的synchronized(对象)
if (map.get(key) != null) {
map.put(key, new SomethingStrictlyUnique());
}
注意:ConcurrentHashMap是弱一致的,在大多数情况下是可以接受的。您设置了什么?使用put和get设置值?或者为hashmap本身创建getter和setter?是的,我的方法包含put和get。