ConcurrentHashMap概述

The primary design goal of this hash table is to maintain concurrent readability (typically method get(), but also iterators and related methods) while minimizing update contention. Secondary goals are to keep space consumption about the same or better than java.util.HashMap, and to support high initial insertion rates on an empty table by many threads.
这个哈希表的主要设计目标是在最小化更新争用的同时保持并发可读性(通常是方法get(),但也包括迭代器和相关方法)。次要目标是保持与java.util相同或更好的空间消耗。HashMap,以及多个线程在空表上支持高初始插入率。

 

This map usually acts as a binned (bucketed) hash table.  Each key-value mapping is held in a Node.  Most nodes are instances of the basic Node class with hash, key, value, and next fields. However, various subclasses exist: TreeNodes are arranged in balanced trees, not lists.  TreeBins hold the roots of sets of TreeNodes. ForwardingNodes are placed at the heads of bins during resizing. ReservationNodes are used as placeholders while establishing values in computeIfAbsent and related methods.  The types TreeBin, ForwardingNode, and ReservationNode do not hold normal user keys, values, or hashes, and are readily distinguishable during search etc because they have negative hash fields and null key and value fields. (These special nodes are either uncommon or transient,so the impact of carrying around some unused fields is insignificant.)

这个映射通常作为一个binned (bucket)哈希表。每个键-值映射都保存在一个Node中。大多数节点都是具有散列、键、值和next字段的基本Node类的实例。但是,存在各种子类:treenode被安排在平衡树中,而不是列表中。treebin持有TreeNodes集合的根。在调整大小期间,forwardingnode被放置在箱子的头部。在computeIfAbsent和相关方法中建立值时,reservationnode被用作占位符。TreeBin、ForwardingNode和ReservationNode类型不保存普通用户键、值或哈希值

 The table is lazily initialized to a power-of-two size upon the first insertion.Each bin in the table normally contains a list of Nodes (most often, the list has only zero or one Node).
Table accesses require volatile/atomic reads, writes, and CASes.  Because there is no other way to arrange this without adding further indirections, we use intrinsics (sun.misc.Unsafe) operations.

表在第一次插入时被延迟初始化为2的幂次大小。表中的每个bin通常包含一个Node列表(通常,这个列表只有0个或一个Node)。
表访问需要volatile/原子读、写和case。因为在不添加进一步间接的情况下,没有其他方式来安排这一点,所以我们使用intrinsic (sun.misc.Unsafe)操作。

We use the top (sign) bit of Node hash fields for control purposes -- it is available anyway because of addressing constraints.Nodes with negative hash fields are specially handled or ignored in map methods.

我们将Node哈希字段的顶部(符号)位用于控制目的——由于寻址限制,它无论如何都是可用的。带有负哈希字段的节点在map方法中会被特殊处理或忽略。

Insertion (via put or its variants) of the first node in an empty bin is performed by just CASing it to the bin.  This is by far the most common case for put operations under most key/hash distributions.  Other update operations (insert,delete, and replace) require locks.  We do not want to waste the space required to associate a distinct lock object with each bin, so instead use the first node of a bin list itself as a lock. Locking support for these locks relies on builtin "synchronized" monitors.

在空容器中插入(通

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值