ConcurrentHashMap和复合操作 [英] ConcurrentHashMap and compound operations
问题描述
Hashtable和Collections.synchronizedMap是线程安全的,但仍然是复合操作
if(!map_obj.containsKey(key)) {
map_obj.put(key,value);
}
需要外部同步作为:
$ b b
同步(map_obj){
只是因为它们保证它们的原始操作也是
if(!map_obj.containsKey(key)){
map_obj.put(key,value);假设我们有ConcurrentHashMap(CHM),而不是Hashtable或者Hashtable,那么我们可以使用ConcurrentHashMap(CHM) HashMap。 CHM为上述复合操作提供了一个替代的putIfAbsent()
方法,因此不需要外部同步。
但是假设没有由CHM提供的
putIfAbsent()
。然后我们可以写下面的代码:synchronized(concurrenthashmap_obj){
if(!concurrenthashmap_obj.containsKey(key) {
concurrenthashmap_obj.put(key,value);
}
}
我的意思是我们可以在CHM对象上使用外部同步吗?它将工作吗?
对于上面的复合操作,在CHM中有
putIfAbsent()
安全用于其他复合操作。我的意思是我们可以使用外部同步CHM对象吗?解决方案否,不能使用外部同步来确保复合操作的原子性
ConcurrentHashMap
。
确切地说,您可以使用外部同步来确保复合操作的原子性,但只有当
c $ c> Collections.synchronizedMap()ConcurrentHashMap
在同一个锁上同步(虽然使用
ConcurrentHashMap
在这种情况下是没有意义的 - 你可以用常规<$ c $使用外部同步的方法使用Hashtable
和<$synchronized
。由于ConcurrentHashMap
不提供这样的保证,原始操作可能会干扰您的复合操作的执行,破坏其原子性。
但是,
ConcurrentHashMap
提供了许多可以用来以乐观的方式实现复合操作的方法:
-
putIfAbsent(key,value)
-
remove / code>
-
replace(key,value)
-
replace(key,oldValue,newValue)
您可以使用这些操作来实现某些化合物操作而不进行解释同步,方式与 AtomicReference
等相同。
Hashtable and Collections.synchronizedMap are thread safe but still compound operations like
if (!map_obj.containsKey(key)) {
map_obj.put(key, value);
}
needs external synchronization as:
synchronized(map_obj) {
if (!map_obj.containsKey(key)) {
map_obj.put(key, value);
}
}
Suppose we have ConcurrentHashMap(CHM) instead of Hashtable or HashMap. CHM provides an alternative putIfAbsent()
method for the above compound operation, thus removing the need of external synchronization.
But suppose there is no putIfAbsent()
provided by CHM. Then can we write following code:
synchronized(concurrenthashmap_obj) {
if (!concurrenthashmap_obj.containsKey(key)) {
concurrenthashmap_obj.put(key, value);
}
}
I mean can we use external synchronization on CHM object?Will it work?
For above compound operation there is putIfAbsent()
method in CHM but how can we achieve thread safety for other compound operations if we are using CHM. I mean can we use external synchronization on CHM object?
No, you cannot use external synchronization to ensure atomicity of compound operations over ConcurrentHashMap
.
To be precise, you can use external synchronization to ensure atomicity of compound operations, but only if all operations with ConcurrentHashMap
are synchronized over the same lock as well (though use of ConcurrentHashMap
won't make sense in this case - you can replace it with regular HashMap
).
Approach with external synchronization works with Hashtable
and Collections.synchronizedMap()
only because they guarantee that their primitive operations are synchronized
over these objects as well. Since ConcurrentHashMap
doesn't provide such a guarantee, primitive operations may interfere with execution of your compound operations, breaking their atomicity.
However, ConcurrentHashMap
provides number of methods that can be used to implement compound operations in optimistic manner:
putIfAbsent(key, value)
remove(key, value)
replace(key, value)
replace(key, oldValue, newValue)
You can use these operation to implement certain compound operations without explict synchronization, the same way as you would do for AtomicReference
, etc.
这篇关于ConcurrentHashMap和复合操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!