ConcurrentHashMap 源码解析(一):解析定义的常量

在解析ConcurrentHashMap源码之前首先需要先弄懂这些常量的定义,这将会对接下来看源码有很大的帮助。这里要注意一点,ConcurrentHashMap1.8中会保留1.7的常量,但是并不会参与到1.8的逻辑计算中。

/**
  * The largest possible table capacity.  This value must be
  * exactly 1<<30 to stay within Java array allocation and indexing
  * bounds for power of two table sizes, and is further required
  * because the top two bits of 32bit hash fields are used for
  * control purposes.
  * 
  * 散列表数组最大限制
  */
 private static final int MAXIMUM_CAPACITY = 1 << 30;

 /**
  * The default initial table capacity.  Must be a power of 2
  * (i.e., at least 1) and at most MAXIMUM_CAPACITY.
  * 
  * 散列表默认值
  */
 private static final int DEFAULT_CAPACITY = 16;

 /**
  * The default concurrency level for this table. Unused but
  * defined for compatibility with previous versions of this class.
  * 
  * 并发级别。1.7遗留下来的,1.8只有在初始化的时候用了一下。并不代表并发级别
  */
 private static final int DEFAULT_CONCURRENCY_LEVEL = 16;

 /**
  * The load factor for this table. Overrides of this value in
  * constructors affect only the initial table capacity.  The
  * actual floating point value isn't normally used -- it is
  * simpler to use expressions such as {@code n - (n >>> 2)} for
  * the associated resizing threshold.
  * 
  * 负载因子。在1.8中ConcurrentHashMap是默认值,与HashMap不同
  */
 private static final float LOAD_FACTOR = 0.75f;

 /**
  * The bin count threshold for using a tree rather than list for a
  * bin.  Bins are converted to trees when adding an element to a
  * bin with at least this many nodes. The value must be greater
  * than 2, and should be at least 8 to mesh with assumptions in
  * tree removal about conversion back to plain bins upon
  * shrinkage.
  * 
  * 树化阈值。指定桶位的链表长度达到8时,有可能发生树化操作
  */
 static final int TREEIFY_THRESHOLD = 8;

 /**
  * The bin count threshold for untreeifying a (split) bin during a
  * resize operation. Should be less than TREEIFY_THRESHOLD, and at
  * most 6 to mesh with shrinkage detection under removal.
  * 
  * 红黑树转化为链表的阈值
  */
 static final int UNTREEIFY_THRESHOLD = 6;

 /**
  * The smallest table capacity for which bins may be treeified.
  * (Otherwise the table is resized if too many nodes in a bin.)
  * The value should be at least 4 * TREEIFY_THRESHOLD to avoid
  * conflicts between resizing and treeification thresholds.
  * 
  * 联合TREEIFY_THRESHOLD控制桶位是否树化,只有当table数组长度达到64且某一个桶位中的链表长度达到8,才会真正树化。
  */
 static final int MIN_TREEIFY_CAPACITY = 64;

 /**
  * Minimum number of rebinnings per transfer step. Ranges are
  * subdivided to allow multiple resizer threads.  This value
  * serves as a lower bound to avoid resizers encountering
  * excessive memory contention.  The value should be at least
  * DEFAULT_CAPACITY.
  * 
  * 线程迁移数据最小步长。控制线程迁移数据最小区间的一个值
  */
 private static final int MIN_TRANSFER_STRIDE = 16;

 /**
  * The number of bits used for generation stamp in sizeCtl.
  * Must be at least 6 for 32bit arrays.
  * 
  * 扩容相关,计算扩容时生成的一个 标识戳
  */
 private static int RESIZE_STAMP_BITS = 16;

 /**
  * The maximum number of threads that can help resize.
  * Must fit in 32 - RESIZE_STAMP_BITS bits.
  * 
  * 65535 表示并发扩容最大线程数
  */
 private static final int MAX_RESIZERS = (1 << (32 - RESIZE_STAMP_BITS)) - 1;

 /**
  * The bit shift for recording size stamp in sizeCtl.
  * 
  * 扩容相关 值恒为16
  */
 private static final int RESIZE_STAMP_SHIFT = 32 - RESIZE_STAMP_BITS;

 /*
  * Encodings for Node hash fields. See above for explanation.
  */
 //node节点的hash值为-1时,表示当前节点为FWD节点
 static final int MOVED     = -1; // hash for forwarding nodes
 //node节点的hash值为-2时,表示当前节点已经树化,且当前节点为TreeBin对象,TreeBin对象代理操作红黑树
 static final int TREEBIN   = -2; // hash for roots of trees
 static final int RESERVED  = -3; // hash for transient reservations
 //0x7fffffff -> 0111 1111 1111 1111 1111 1111 1111 1111 可以将一个负数通过位与运算变为正数,但并不是取绝对值。
 static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash

 /** Number of CPUS, to place bounds on some sizings
  * 当前系统的cpu数量
  * */
 static final int NCPU = Runtime.getRuntime().availableProcessors();

 /** For serialization compatibility.
  * 1.8 序列化为了兼容1.7 ConcurrentHashMap保存的
  * */
 private static final ObjectStreamField[] serialPersistentFields = {
     new ObjectStreamField("segments", Segment[].class),
     new ObjectStreamField("segmentMask", Integer.TYPE),
     new ObjectStreamField("segmentShift", Integer.TYPE)
 };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值