为什么ConcurrentHashMap不能被锁定为独占访问? [英] Why ConcurrentHashMap cannot be locked for exclusive access?
问题描述
来自 #JCIP 的报价:
由于ConcurrentHashMap不能被锁定为独占访问,我们
不能使用客户端锁定创建新的原子操作,如
put-if-absent ,就像我们对Vector做的一样:
"Since a ConcurrentHashMap cannot be locked for exclusive access, we cannot use client-side locking to create new atomic operations such as put-if-absent, as we did for Vector"
为什么我们不能获取锁定 线程安全(例如 Collections.synchronizedxxx 工厂返回的同步集合)的原子方法:
Why we can't just acquire the lock in order to implement additional atomic methods and keep the collection thread-safe (like synchronized collections returned by Collections.synchronizedxxx factory) :
推荐答案
为什么?因为实现不支持它。 直接从 ConcurrentHashMap
JavaDocs:
Why? Because the implementation does not support it. Straight from the ConcurrentHashMap
JavaDocs:
有不支持锁定整个以阻止所有访问的方式
There is not any support for locking the entire table in a way that prevents all access
...根据定义,这是独占访问。
...which is, by definition, "exclusive access."
这篇关于为什么ConcurrentHashMap不能被锁定为独占访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!