Why ConcurrentHashMap cannot be locked for exclusive access?

后端 未结 3 1680
旧巷少年郎
旧巷少年郎 2021-01-21 02:31

A quote from #JCIP :

\"Since a ConcurrentHashMap cannot be locked for exclusive access, we cannot use client-side locking to create new atomic operatio

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-21 03:01

    The whole point of the ConcurrentHashMap is that read operations never block, i.e. do not have to check for locks. That precludes the ability to have such a lock.

    Why we can't just acquire the lock :

    You could do that, but you have to do it consistently for all access paths to the map, and then you have completely negated to purpose of a concurrent data structure. It is supposed to be lock-free.

提交回复
热议问题