ConcurrentHashMap源码剖析 (8)

ConcurrentHashMap源码剖析

2.6 jdk1.8集合长度获取 public int size() { long n = sumCount(); return ((n < 0L) ? 0 : (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int)n); }

sumCount方法

final long sumCount() { CounterCell[] as = counterCells; CounterCell a; //获取baseCount的值 long sum = baseCount; if (as != null) { //遍历CounterCell数组,累加每一个CounterCell的value值 for (int i = 0; i < as.length; ++i) { if ((a = as[i]) != null) sum += a.value; } } return sum; }

注意:这个方法并不是线程安全的

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpsdsy.html