一, 什么是ConcurrentHashMap

ConcurrentHashMap和HashMap一样是一个用来存储键值对<key,value>的集合类,但和HashMap不同的是elementuiConcurrentHashMap是线程安全的,也就是多个线程同时对ConcurrentHashMap进行修改或者删除增加操作不会出现数据错误的问题.

二, 实现原理

和HashMap一样采用安全期怎么算数组+链表+红黑树实现

但和HashMap不同的安全手抄报是,数组中存储的安全教育节点类型有所增加,包括Node<key,value>,TreeNode<key,value>,Forwardinelementary翻译gNode<key,value>,新增这个节点的目的就是为了线程并发协elementary助扩容时使用

深入理解ConcurrentHashMap

三, 基本属性介绍

//011111111111111安全手抄报1elementui1111安全111111111111 该值elementary翻译可以保证计算出安全期安全期的哈希值为正数
static final int HASH_BITS = 0x7fffffff; // usable bits of nor安全mal node hash
//该属性用在扩容时生成一个负值,表示正在扩容
//The number of bits used for generation stamp in sizeCtl.
//sizeCtl中用于生成戳记的位数。
//Must be at least 6 for 32bit arrays.
//对于32位数组,必须至少为6。
private安全期是哪几天 static in安全期t RESIZE_ST安全AMP_BITS = 16;
//和上面一样,也是为了在扩安全教育平台登录入口容时生成一个负值,具体在代码中解释
//The bit shift for recordi安全手抄报ng size stamp in sizeCtl.
//在s安全izeCtl中记录大小戳的位移位。
private static final int RESIelementary是什么意思ZE_STAMP_SHIFT安全 = 32 - RESIZE_STAMP_BITS;
//表示当前桶位正在被迁安全教育平台
//Encodings for Node hash fields. See above for explanation.
static final int MOVED = -1;
//表示当前桶是以树来存储节点的
static finaelementary怎么读音l int TREEBIN = -2;
//Number of CPUS, to plelementaryace bounds on some sizings
//cpu的数量,用来计算元素数量时限制CounterCell数组大小
static final int NCPU = Runtime.getRuntime().availableProcessors();
/**
* The next table to use; non-null onelementuily while resizing.
* 用来扩容的哈希表
*/
private transient volati安全教育平台登录入口le Node<K, V>[] nextTable;
/**
* Base counter value, used mainly安全期计算器 when there is no contention, but alelement是什么牌子so as a fallbaelementuick during table    initElementialization
* races. Up安全期是哪几天datedelementary是什么意思 via CAS.
* 哈希表元素elements数量,通过lo安全期是月经后几天ngAdderelements来维护
*/
private transient volatile long baseCount;
/**
*安全期是月经后几天 Table initial安全期计算器ization an安全教育平台登录入口d resizing control.
* 哈希表初始化和扩容大小控制.
* When negative, the table is being initialized or reelement什么意思中文sized:
* 当这个值为负数时,表示哈希表正在初始化或重新计算大小
* -1 for initializ安全教育平台登录入口ation,
* -1 表示正elementui在初始化了
* else -(1 + the number of activeelements resizing threads).
* 表示哈希表正在扩容,-(1+n),表示此时有n个线程正在共同完成哈希表的扩容
* Otherwise, when table is null, holds the initial taelementary怎么读音ble size to use upon cr安全期是哪几天eation,安全期是月经后几天or 0 for default.
* 否则,当哈希表为空时, 保留要创建哈希表的大小0或默认(16)
* After initialization, holds the next element coun安全期是月经后几天t value upon which to resize the table.
* 初始化完成之后安全教育平台,保留下一次需要扩容的阈值
*/
private transient volatile int sizeCtl;
/**
* Telementaryhe next table index (pluelementarys one) to split while resizielementanimationng.
* 扩容时的当前转移下标
*/
private transient volatile int transferIndex;
/**
* Sp安全教育平台登录入口inlock (locked via CAS) used when resizing and/or creating CounterCells.
* 获取计算集安全合元素容量的CounterCell对象的锁
*/
priva安全te transient volatile int cellsBelement是什么牌子usy;
/**
* Table of counter cells. When non-null, size is a power of 2.
* 计算元素数量elementary翻译的数组
*/
private transient volat安全期是哪几天ile CounterCell安全期是哪几天[] countelement什么意思中文erCellselementui;

四, 构造函数

/*element是什么牌子*
* 和HashMap构造函数不同的是安全手抄报,数组容量的计算总是大于传入容量的2的幂
* 即如果传入32则数组初始容量为64,而不是32,而Hashelementary翻译Map计算出来为32elements
*/
public ConcurrentHashMap(int initialCapacelement是什么牌子ity) {
if (initialCapacity < 0)
throw new IllegalArgumentException();
int cap = ((initialCapacity >= (MAXIMUM_C安全APACITY >>>elementary是什么意思 1)) ?
MAXIMUM_CAPACITY : tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
this.sizeCtl = cap;
}

五, 常用方法介绍

/**
* 这个方法就是HashMap中的hash方法,用来计算哈希值
*/
static finalElement int spread(int h) {
return (h ^ (h >>&elementarygt; 16)) & HASH_BITS;
}

获取节点

深入理解ConcurrentHashMap

public V get(Object key)安全期是哪几天 {
Node<K, V>[] tab;
Node<K, V> e, p;
int n, eh;安全期
K ek;
//计算散列elementary翻译
int h = sp安全期计算器read(key.hashCode());
//安全期计算器计算下标(这一块同HashMap不再赘述)
if ((tab = table) != null &amelement什么意思中文p;&element滑板 (nelementanimation = tab.length) > 0 &a安全教育mp;& (e = tabAt(tab, (n - 1) & h)) != null) {
if ((eh = e.helementary翻译ash) == h) {
if ((ek = e.key) ==安全教育平台登录入口 key || (ek != null && key.equals(ek)))
return e.val;
} else if (eh &安全教育lt; 0)
//哈希值小于0,表示为树节点,从树中寻找,这一步和HashMap一致
return (p = e.find(h, key)) != null ? p.val : null;
//在链表中寻找
while ((e安全教育 = e.next) != null) {
if (e.hash == h &aelementanimationmp;& ((ek = e.key) == key || (ek != null && key安全期.equals(ek))))
return e.val;
}
}
return null;
}

插入节点

深入理解ConcurrentHashMap

public V put(K key, V value) {
returelementary是什么意思n putVal(key, value, fal安全期se);
}
final V putVal(K key, V value, boolean onlyIfAbsent) {
if (key == null || valueelementary翻译 == null) throw new NullPointerException()element是什么牌子;
//计算哈希值
int hash = spread(key.hashCode());
//插入桶的节点数量
int binCount = 0;
//使用死循环,目的是可能有的线程正在协助扩容,之后还需要插入或者更新,或者需要操作的节点所在的桶已经被其他线程锁定,需要等待其他线程执行完之后再执行
for (Node<K, V>[] tab = table; ; ) {
Node<K, V> f;
int n, i, fh;
if (tab == null || (n = tab.length) == 0)
//初始化哈希表
tab = initTable();
else if安全教育平台登录入口
//计算下标,并且计算该下安全期怎么算标是否有元素
((f = tabAt(t安全教育ab, i = (n - 1) & hash)) == null) {
//cas插入,这一Element步不需要锁,因为elementui当前桶为空
if (casTabAt(tab, i, null, new Node<K, V>(hash, key, value, null)))
break;
} else if
//安全期是哪几天代表当前节点已经被移动,正在扩容,需要element什么意思中文当前线程协助扩容
((fh = f.hash) == MOVED)
tab = helpTranelementssfer(tab, f);
else {
V oldVal = null;
//锁住头节点,保证所有线程的插入都是线程安全的
syn安全期计算器chronized (f) {
//这一步判断的原因是,可能插入元素之后会造成链表树化,安全期怎么算需要插入的位置已经发生了变化
if (tabAt(tab, i) == f) {
if (fhelementanimation >= 0) {
//当前桶上有多少个节点
binCountElement = 1;
for (Node<K, V> e = f; ; ++binCount) {
K ek;
//查找到了key相同的节点,直接修改值并返回旧的值
if (e.hash == hash && ((ek = e.key) == key ||安全期计算器 (ekelementui != null && key.equals(ek)))) {
oldVal = e.val;
if (!onlyIfAbsent)
e.val = value;
break;
}
Node<K, V> pred = e;
//没有找到相同的key,直接向链表尾部插入节点
if ((e =安全期怎么算 eelementui.nelementaryext) == null) {
p安全red.next = new Node<K, V&gt安全教育平台登录入口;(h安全期怎么算ash, key, value, null);
break;
}
}
} else if (f instanceof TreeBin) {
//给树里面插入节点
Node<K, V> p;
binCount = 2;
if ((p = ((TreeBin<K, V>) f).putTreeVal(hash, key, value)) != null) {
oldVal = p.val;
if (!onlyIfAbsent)
p.val = value;
}
}
}
}
if (binCount != 0) {
//需要树化
if (binCount >= TREEIFY_THRESHOLD)
treeielement是什么牌子fyB安全期是月经后几天in(tab, i);
//修改旧值,直接将旧值返回
if (oldVal != null)
return oldVal;
break;
}
}
}
//计算节点数量
addCount(1L, binCount);
return nul安全l;
}

初始化哈希表

深入理解ConcurrentHashMap

private final Node&Elementlt;K, V>[] initTablelement滑板e() {
Node<Kelementary, V>[] tab;
int selementary翻译c;
while ((tab = table) == null || tab.length == 0) {
//小于0表示正在初始化或者正在扩容,让出cpu
if ((sc = sizeCtl) < 0)
Thread.yield(); // lost initialization race; just spin
else if
//判断sc是否与SIelementaryZECTL是否相等,如果相等,则将SIZECTL设置为-1,表示当前正在初始化(只有一个线程能进行此操作,其他线程会被挡在前面的判断上)
(U.compareAndSwapInt(this, SelementaryIZECTL, sc, -1)) {
try {
//防止有线程已经初始化安全期是月经后几天了1
if ((tab = table) == n安全期是哪几天ull || t安全ab.length == 0) {
//该sc如果在构造器上传入了,则会被计算为大于其的2次幂,否则会按照默认值初始化
int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
@SuppressW安全期是哪几天arnings("unchecked")安全教育平台登录入口
Node<K, V>[] nt = (Node<K, V>[]) new Node<?, ?>[n];
table = tab = nt;
//设置下一次扩容的阈值 n - (n >>> 2) = n - n / 4 = (3 / 4) * n = 0.75n,即下一次的扩容elementui阈值Element为当前哈希表数量的0.75*n
sc = n - (n &安全教育平台登录入口gt;>> 2);
}
} finally {
//设置sizeCtl为-1,表示初始化动作已经有线程在执行了
sizeCtl = sc;
}
break;
}
}
return tab;
}

计算节点数量

private final void addCount(long x, int check) {elements
CounterCell[] as;
long b, s;
/*
* 维护数组长度安全教育平台登录入口
*/
//尝试cas直接修改值,如果修改失败
if ((as =安全教育平台 counterCells) != null || !U.安全期compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) {
CounterCe安全期是月经后几天ll a;
long v;
int m;
boolean uncontended = true;
//数组为空或者长度小于0或者对应的位置为空或者直接修改数组对应位置上的值失败,则进行修改操作
if (as == null || (m = as.length - 1) < 0 || (a = as[ThreadLocalRandom.geelementary是什么意思tProbe() & m]) == null ||
!(uncontended = U.compareAndSwelement什么意思中文apLong(a, CELLVALUE, v = a.value, v + x))) {安全期是哪几天
fullAddCount(x, uncontended);
return;
}
//桶上的节点数安全期是月经后几天量小于等于1,不需要判断扩容,直接退出
if (check <= 1)
return;
//elementary怎么读音获取当前数组的元素数量
s = sumCount();
}
/*
* 判断是否需要扩容
*/
if (check >= 0) {
Node<K, V>[] tab, nt;
int n, sc;
//当前节点数量大于扩容阈值,并且数组不为空并且数安全手抄报组长度小于最大值则需要扩容
while (s >= (long) (sc = sizeCtl) && (taelement是什么牌子b = table) != nu安全教育ll &&安全教育平台登录入口 (n = tab.length) &安全lt; MAXI安全手抄报MUM_CAPACITY) {
//获取一个负值
int rs = resizeStamp(n);
//如果sc小于0,说明正在扩容,需要协助扩容
if (sc < 0) {
//判断扩容安全期是否完成
if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 |elementary怎么读音| sc == rs + MAX_安全RESIZERS || (nt = neelementanimationxtTable) == null || transferIndex <= 0)
breakelement是什么牌子;
//协助扩容elements,这里sc+1代表新加入一个线程协助扩容
if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))
transfer(tab, nt安全期是哪几天);
} else if
/*
* 假设 rs = 00000000 00000000 10000000 00000000
* 将其向左移16位结果为 10000000 00000000 00000000 00000000 可以看出该值为负
* 这一步尝试elementary将sc设置为负数
*/
(U.compareAndS安全教育平台wapInt(this, SIZEelementary翻译CTL, sc, (rs << RESIZE_STAMP_SHIFT) + 2))
//将旧数组置空,里面会创建一个新的数组
transfer(tab, null);
//计算集合元素数量
s = sumCount();
}
}
}
priv安全教育平台ate fina安全期是月经后几天l void fulelement是什么牌子lAddCount(l安全教育平台登录入口ong x, boolean wasUncontended) {
int h;
//获取当前线程的hash值
if ((h = ThreadLocalRandom.getProbe()) == 0) {
ThreadLocalRandom.localInit();      // force initialization
h = ThreadLocalRand安全om.getProbe();
wasUncontended = t安全教育平台登录入口rue;
}
//检测是否有冲突,如果最后一个桶不为nulElementl,则为t安全期计算器rue
boolean collide = false;
for (; ; ) {
CounterCell[] as;
CounterCell a;
int n;
long v;
//数组如果不为空,elementary则优先对Counte安全期rCell里面的counterCell的value进行累加
if ((as = count安全期是哪几天erCells) != null && (n = as.length) > 0) {
//当前位置为空
if ((a = as[(n - 1) & h]) == null) {
//当前没有线程尝试修改该值
ifelements (cellsBusy == 0) {
CounterCell r = n安全期是哪几天ew Counteelement是什么牌子rCell(x);
//抢占修改的锁
if (cellsBusy == 0 && U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
boolean createdelementary怎么读音 = false;
try {
CounterCell[] rs;
int m, j;
if ((rs = counterCells) != null &&ampelement是什么牌子; (m = rs.length) > 0 && rs[j = (m - 1) & h] == null) {
rs[j] = r;
created = true;
}
} finally {
//释放锁
cellsBusy = 0;
}
if (created)
break;
continue;           // Slot is now non-empty
}
}
//抢占失败安全期是月经后几天
collide = false;
} else if
//桶位不为空,重新安全计算elementary是什么意思线程has安全期是哪几天h值,继续循环
(!wasUncontended)       // CAS already known to fail
wasUncontended = true;      // Continue after rehash
/*
* 重新计算ha安全期sh值之后,对应的桶位还是不为空,对value进行累加
* 尝试cas对value加值
*/
else if (U.compareAndSwapLong(aelementary翻译, CELLVALUE, v = a.value, v + x))
break;
//数组长度已经大于等于CPU的核数安全了,不需要再扩容了
else if (counterCells != as || n >= NCPU)
collide = false;element是什么牌子
//当没有冲突,修改为有冲突,重新计算hash值,继续循环
else if (!collid安全期是月经后几天e)
collide = true;
else if
//多次循环没有设置成功值,则对安全期计算器原数组element什么意思中文进行扩容
(cellelement滑板sBusy == 0 && U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
try {
if (counterCells == as) {// Ex安全期是哪几天pand table unless stale
//数elementary是什么意思组长度没有超过cpu核数,将数组扩容两倍
CounterCell[] rs = new CounterCell[n << 1];
for (int i = 0; i < n; ++i)
//扩容使用
rs[i] = as[i];
c安全期是月经后几天ounterCells = rs;
}
} finally {
cellsBusy = 0;
}
collide = false;
continue;                   // Retry with expandeelementaryd table
}
//重新计算随机值
h = ThreadLocalRandom.adva安全nceProbe(h);
} else if
//初始进来数组为空,需要初始化数组
(cellsBusy == 0 && counterCells == as && U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
boolean init = false;
try {
if (counterCells == as) {
CounterCell安全[] rs = n安全期怎么算ew CounterCell[2];
rs[h & 1] = new CounterCell(x);
counterCel安全ls = rs;
init = true;
}
} finally {
cellsBusy = 0;
}
if (init)
break;
} else if
//数组为空并且有其他线elementary是什么意思程正在创建数组,尝试直接对baseCount进行累加
(U.compareAndSwapLong(this, BASECOUNT, v = baseCount, v + x))
break;                          // Fall安全教育平台登录入口 back on using base
}
}

扩容并迁移

深入理解ConcurrentHashMap

private final void transfer(Node<K, V>[] tab, Node<K, V>[] nextTab) {
//stride表示迁移数据的区间
int n = tab.length, stride;elementary翻译
/*
* 这里计算每个CPU负责迁移元素的个数
* 如果这里的跨度区间小于16,则按照最小区间16来计算
*/
if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
stride = MIN_TRANSFER_STRIDE;
//这里表示为第一个线程来扩容
if (nextTab == null) {
try {
//扩容为两倍
@SuppressWarnings("unchecked")
Node<K, V>[] nt = (Node<K, V>[]) new Node<?, ?>[n << 1];
nextTab = nt;
} catch (Throwable ex) {
sizeCtl = Integer.MAX_VALUE;
retelement什么意思中文urn;
}
next安全教育平台Table = nextTab;
//迁移数据的element是什么牌子index
transferIndex = n;
}
//新扩容数组的长度
int next安全n = nextTab.length;
//创建头节点,该节点会被标识为MOVED表示数据正在迁移中
ForwardingNode<K, V> fwd = new ForwardingNode<K, V>(nextTab);
boolean advance = true;
boolElementean finishing = false; // to ensure sweep before committing nextelementanimationTab
//从后向前迁移
for (int i = 0, bound = 0; ; ) {
Node<K, V> f;
int fh;
while (advance) {
int nextIndex, nextBoelement什么意思中文und;
//不属于自己的迁移位置或者已经迁移完成直接退出
if (--i >= bound || finishing)
adva安全教育nce = false;
else if
//下一个迁移位置小于等于0直接退出
((nextIndeelementaryx = transferIndex) <= 0) {
i = -1;
advance = false;
} else安全期计算器 if
//计算迁移位置(多线程会划分多安全个区间)
(U.elementary翻译compareAndSwapInt(this, TRANSFERINDelementsEX, nextIndex, nextBound = (nextIndex > stride ? nextIndex - stride : 0))) {
bound = nextBound;
i = nextIndex - 1;
advance = false;安全期怎么算
}安全期是哪几天
}
if (i < 0 || i >= n || i + n &安全期计算器gt;= nextn) {
int sc;
//判断是否所安全期是月经后几天有的线安全教育程都做完了任务
if (finishing) {
nextTable = null;
table = nextTab安全;
//等于0.75 * 2n,也就是新数组扩容2倍*扩容因子
sizelement什么意思中文eCtl = (n <&安全期计算器lt; 1) - (n >>> 1);
return;
}
if (U.compareAndSwapInt安全期(this, SIZECTL, sc = sizeCtl, sc - 1)) {
//判断扩容是否成功
if ((sc - 2)安全期计算器 != resizeStamp(n) << RESIZE_STAMP_SHIFT)
return;
finishing = advance = true;
i = n; // recheck before comelementanimationmit
}
} else if
//如果当前位置为空,直接插入fwd节点,elements表示当前节点正在被迁移
((f = tabAt(tab, i)) == null)
advance = casTabAt(tab, i, null, fwd);
else if ((安全期是哪几天fh = f.hash) == MOVED)
//正在被迁移,需要重新计算位置
advance = true; // already processed
else {
/elementary怎么读音/迁移代码
synchronized (f) {
ielementsf (tabAt(tab, i) == f) {
Node<K, V> ln, hn;
if (fh >= 0) {
int runBit = fh & n;
Node<K, V> lastRun = f;
for (Node<K, V> p = f.next; p != null; p = p.next) {
int b = p.hash & n;
if (b != runBit) {
runBit = b;
lastRun = p;
}
}
if (runBit == 0) {
ln = l安全教育astRun;
hn = null;
} else {
hn = lastRun;
ln = null;
}
for (Node<K, V> p = f; p != las安全tRu安全教育平台登录入口n; p = p.next) {
int ph = p.hash;
K pElementk = p.key;
V pv = p.val;
if ((ph & n) == 0)
ln = new Node<K, V>(ph, pk, pv, ln);
else
hn = new Node<K, V>(ph, pk, pv, hn);
}
setTabAt(nextTab, i, ln);
setTabAt(nextTab, i + n, hn);
//迁移完成,设置头节点为fwd
setTabAt(tab, i, fwd);
advance = true;
} else if (f instanceof TreeBin) {
T安全期怎么算reeBin<K, V> t = (TreeBin<K, V>) f;
TreeNode&element什么意思中文lt;K, V> lo = null, loTail = null;
TreeNode<K, V>安全期是哪几天 hi = null, hiTail = null;
int lc = 0, hc = 0;
for (Node<K, V> e = t.first安全期怎么算; e != null; e = e.next) {
int h = e.hash;
TreeNode<K, V> p = new TreeNode<K, V>
(h, e.key, e.val, null, null);
if ((element滑板h & n) == 0) {element什么意思中文
if ((p.prev = loTail) == null)
lo = p;
else
loTail.next = p;
loTail = p;
++lc;
} else安全手抄报 {
if ((p.prev = hiTail) ==elementary是什么意思 null)
hi = p;
else
hiTail.next = p;
hiTail = p;
++hc;
}
}
ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) :
(hc != 0) ? new TreeBin<K, V>(lo) : t;
hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) :
(lc安全期是月经后几天 != 0) ? new TreeBin<K, V>(hi) : t;
setTabAt(nextTab, i, ln);
setTabAt(elementary是什么意思nextTab, i + n, hn);
//迁移完成,设置头节点为fwd
setTabAt(tab, i, fwd);
//重新计算位置继续迁移
advance = true;
}
}
}
}
}
}

获取节点数量

public int size() {
long n = sumCount();
return ((n < 0L) ? 0 : (n > (安全期是月经后几天long) Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int) n);
}
/**
* 获取哈希表中节点的数量(非线程安全)
* 这个方法返回的数据不一定准确,因为可能在调用该方法的时候,有其他线程正在尝试给数elementary组中的节点加值
*/
final long sumCount() {
CounterCell[] as = counterCells;
CounterCell a;
long sum =element滑板 baseCount;
if (as != null) {
for (CounterCell counterCell : as) {
if ((a = counterCell) != null)
sum += a.value;
}
}
return sum;
}