标签:Node ConcurrentHashMap hash HashMap int 1.8 next tab null
HashMap
HashMap JDK1.7 数据结构图
HashMap JDK1.8 数据结构图
HashMap JDK1.8 put 数据流程图 (泳道图)
HashMap源码解释
HashMap类
public class HashMap<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable {
}
1.单继承自AbstractMap,多实现Map,Cloneable..
HashMap成员变量
//默认初始容量为 16= 位运算2的4次方
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
//最大容量 2的30次方
static final int MAXIMUM_CAPACITY = 1 << 30;
//默认扩容加载因子
static final float DEFAULT_LOAD_FACTOR = 0.75f;
//链表转红黑树的阈值 等于9的时候才考虑是否转红黑树
static final int TREEIFY_THRESHOLD = 8;
//红黑树转链表的阈值
static final int UNTREEIFY_THRESHOLD = 6;
//可以树形化的容器的最小表容量。JDK1.8链表转红黑树时候的容量前提条件
static final int MIN_TREEIFY_CAPACITY = 64;
- 默认初始容量为 16= 位运算2的4次方,不直接写16是因为位运算的效率更高。
- 默认扩容加载因子是0.75,是因为考虑空间时间折中数字,有网友用牛顿二项式计算出适合 扩容的因子是0.6913
- 链表转红黑树的阈值设置为8是因为波尔分布概率,当链表长度为8时的概率为
0.00000006极低的概率,是考虑海量数据或人为干扰,变换数据结构为红黑树,此时提升性能的体现是海量数据。
- JDK1.8链表转红黑树时候的容量前提条件是数组的容量大于64
HashMap放入元素时计算数组下标(不变)
//返回hash
//(h = key.hashCode()) ^ (h >>> 16) 把object方法中取出的hashcode进行位或运算返回
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
//如果数组的(n - 1) & hash 位置是空的 :
//n = (tab = resize()).length; n是数组长度-1
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
//(n - 1) & hash hash值与(数组长度-1)进行与运算才是数组下标的值
- hashcode与16进行位或运算作为hash值
- hash值再与(数组长度-1)进行与运算 作为最终数组下标值。
- 为什么与16进行位或运算?
- 因为效率更高
- 为什么(数组长度-1)?
- 因为数组长度是2的幂次方值,任何二进制数与其或运算后都会是在数组的头或尾部。如果减一,与运算结果可以散列在0-15之间
HashMap放入重复key怎么办?(不变)
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
HashMap扩容时候怎么办?(重点变化)
JDK1.7
- 当前容量>总容量 * 0.75
- 为什么是0.75?源码中注释有介绍:是考虑到时间和空间的折中数字。
- 扩容到原容量的2倍,保证2的指数次幂。
- 移动原来元素去新数组(扩容后的新容量)。源码见transfer方法如下,会导致多种问题。
void transfer(Entry[] newTable, boolean rehash){
int newCapacity = newTable.length;
//遍历原数组,
for (Entry<K, V> e : table) {
//链表遍历
while (null != e) {
// 第一行
Entry<K, V> next = e.next;
if (rehash) {
//再次hash运算--->影响效率
e.hash = null == e.key ? 0 : hash(e.key);
}
int i = indexFor(e.hash, newCapacity);
// 第二行
e.next = newTable[i];
// 第三行
newTable[i] = e;
// 第四行
e = next;
}
}
}
问题点:
- 再次hash的效率问题。
- 移动过程中链表的先后顺序会颠倒,因为采用头插法导致。(第一行、第二行..第四行)的指针移动导致。
- 多线程情况下:某一线程执行后链表的先后顺序会颠倒,另一线程并发移动时重新构建链表后链表呈循环状态(即链表末尾元素的next指针会指向头节点而不是null),导致再头插法进新元素时遍历链表会进入死循环。
JDK1.8
- 当前容量>总容量 * 0.75 。扩容到原容量的2倍,保证2的指数次幂。移动原来元素去新数组(扩容后的新容量)(没有改变思路)
- 【变化的】是扩容时候移动的代码,如下
else { // preserve order
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
改进点:
- 去掉再次hash定位,效率提升。取而代之的是高地位指针【前提:数组的长度保持2的幂次方】。
- 高位Node<K,V> hiHead = null, hiTail = null;
- 如果(e.hash & 旧容量) == 0 用低位指针
- 低位Node<K,V> loHead = null, loTail = null;
- 如果(e.hash & 旧容量) != 0 用高位指针
- 经过循环后会产生两个链表,
- 低位链表移到新数组的与旧数组【同样位置】
- 高位链表移到【同样位置+旧的容量 = 新位置】
- 高位Node<K,V> hiHead = null, hiTail = null;
- 尾插的,此思路有效避免循环链表问题。
- 此思路适用于分库分表在线扩容。
ConcurrentHashMap
ConcurrentHashMap JDK 1.7 数据结构
ConcurrentHashMap JDK 1.8 数据结构
ConcurrentHashMap 原理
注:put操作(写)才与hashmap做区别
ConcurrentHashMap 对比
HashMap | ConcurrentHashMap1.7 | ConcurrentHashMap1.8 | |
---|---|---|---|
数据结构 | 数组+链表+红黑树 | segment数组+HashMap1.7 | 数组+链表+红黑树 |
线程安全 | 无 | segment实现ReentrantLock | 基于synchronized+CAS |
put操作 | 直接添加元素 | 先tryLock()加锁 再 添加元素 | cas初始化 +cas插入空数组位 +synchronized插入fei kong |
锁的粒度 | 无 | 分段锁: 锁了一张hash表【粒度大】 | 锁的是桶(数组/红黑树) |
ConcurrentHashMap JDK 1.8 源码
ConcurrentHashMap重要成员变量
static final int MOVED = -1; // hash for forwarding nodes
static final int TREEBIN = -2; // hash for roots of trees
static final int RESERVED = -3; // hash for transient reservations
static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash
- MOVED: 值为-1, 当Node.hash为MOVED时, 代表着table正在扩容。
- TREEBIN, 置为-2, 代表此元素后接红黑树。
/**
* 容器数组。在第一次插入时延迟初始化。大小总是2的幂
*/
transient volatile Node<K,V>[] table;
/**
* 下个可用table; 扩容不为空前提下
*/
private transient volatile Node<K,V>[] nextTable;
/**
* 基本计数器值,主要在没有争用时使用。被CAS更新
*/
private transient volatile long baseCount;
/**
* 表初始化和调整大小控件。
* 当为负数时,表示表正在初始化或调整大小
*/
private transient volatile int sizeCtl;
/**
* table容量从n扩到2n时, 是从索引n->1的元素开始迁移, transferIndex代表当前已经迁移的元 素下标
*/
private transient volatile int transferIndex;
sizeCtl
- 0: table还没有被初始化
- -1: table正在初始化
- 小于-1: table正在扩容
- 大于0: 初始化完成后, 代表table最大存放元素的个数, 默认为0.75*n
ConcurrentHashMap put方法源码
final V putVal(K key, V value, boolean onlyIfAbsent) {
if (key == null || value == null) throw new NullPointerException();
int hash = spread(key.hashCode());
int binCount = 0;
//循环cas获取执行权***************************************************//
for (Node<K,V>[] tab = table;;) {
Node<K,V> f; int n, i, fh;
if (tab == null || (n = tab.length) == 0)
//initTable()中****************************************************//
//cas获取执行权初始化************************************************//
tab = initTable();
else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
//cas无锁put元素进空位***********************************************//
if (casTabAt(tab, i, null,
new Node<K,V>(hash, key, value, null)))
break; // no lock when adding to empty bin
}
//MOVED为-1标识正在扩容**********************************************//
//意味着正在向新表移动************************************************//
else if ((fh = f.hash) == MOVED)
tab = helpTransfer(tab, f);
else {
V oldVal = null;
//synchronized锁非空位**********************************************//
//带锁去构建/追加 链表 或者 红黑树*************************************//
//非空位上的元素作为锁************************************************//
synchronized (f) {
//构建/追加 链表 ****************************************************//
if (tabAt(tab, i) == f) {
if (fh >= 0) {
binCount = 1;
for (Node<K,V> e = f;; ++binCount) {
K ek;
if (e.hash == hash &&
((ek = e.key) == key ||
(ek != null && key.equals(ek)))) {
oldVal = e.val;
if (!onlyIfAbsent)
e.val = value;
break;
}
Node<K,V> pred = e;
if ((e = e.next) == null) {
pred.next = new Node<K,V>(hash, 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)
treeifyBin(tab, i);
if (oldVal != null)
return oldVal;
break;
}
}
}
- 初始化hahsmap用CAS保证线程安全。
- 数组中为空时候,cas添加元素。
- 数组中不为空时候synchronized添加元素到链表/红黑树。
- 一个线程插入数据时, 发现table对应元素的hash=MOVED, 那么调用helpTransfer()协助扩容。如下:
ConcurrentHashMap 扩容方法源码——协助扩容逻辑
final Node<K,V>[] helpTransfer(Node<K,V>[] tab, Node<K,V> f) {
Node<K,V>[] nextTab; int sc;
if (tab != null && (f instanceof ForwardingNode) &&
(nextTab = ((ForwardingNode<K,V>)f).nextTable) != null) {
int rs = resizeStamp(tab.length);
while (nextTab == nextTable && table == tab &&
(sc = sizeCtl) < 0) {
if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
sc == rs + MAX_RESIZERS || transferIndex <= 0)
break;
if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1)) {
//转移************************************************************************//
transfer(tab, nextTab);
break;
}
}
return nextTab;
}
return table;
}
ConcurrentHashMap 扩容方法源码——实际扩容逻辑
private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
int n = tab.length, stride;
if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
//每个线程最少迁移16个数组位**********************************************//
stride = MIN_TRANSFER_STRIDE;
if (nextTab == null) {
try {
@SuppressWarnings("unchecked")
//扩容2倍**************************************************************//
Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n << 1];
nextTab = nt;
} catch (Throwable ex) { // try to cope with OOME
sizeCtl = Integer.MAX_VALUE;
return;
}
nextTable = nextTab;
transferIndex = n;
}
int nextn = nextTab.length;
ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
boolean advance = true;
boolean finishing = false;
for (int i = 0, bound = 0;;) {
Node<K,V> f; int fh;
while (advance) {
int nextIndex, nextBound;
//本线程没有处理完成,那么就继续处理************************************//
if (--i >= bound || finishing)
advance = false;
else if ((nextIndex = transferIndex) <= 0) {
i = -1;
advance = false;
}
else if (U.compareAndSwapInt
(this, TRANSFERINDEX, nextIndex,
nextBound = (nextIndex > stride ?
nextIndex - stride : 0))) {
bound = nextBound;
i = nextIndex - 1;
advance = false;
}
}
if (i < 0 || i >= n || i + n >= nextn) {
int sc;
//如果迁移完成*******************************************************//
if (finishing) {
nextTable = null;
table = nextTab;
sizeCtl = (n << 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 commit
}
}
else if ((f = tabAt(tab, i)) == null)
advance = casTabAt(tab, i, null, fwd);
//别的线程已经在处理***********************************************//
else if ((fh = f.hash) == MOVED)
advance = true;
//如果数组位置上有元素***********************************************//
else {
//需要加锁,防止再向里面放值。
//另外一个线程向这个节点插入数据,此时迁移到这里了,会被阻塞住
synchronized (f) {
if (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 = lastRun;
hn = null;
}
else {
hn = lastRun;
ln = null;
}
for (Node<K,V> p = f; p != lastRun; p = p.next) {
int ph = p.hash; K pk = 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);
setTabAt(tab, i, fwd);
advance = true;
}
// 如果是红黑树***************************************//
else if (f instanceof TreeBin) {
TreeBin<K,V> t = (TreeBin<K,V>)f;
TreeNode<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 ((h & n) == 0) {
if ((p.prev = loTail) == null)
lo = p;
else
loTail.next = p;
loTail = p;
++lc;
}
else {
if ((p.prev = hiTail) == 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(nextTab, i + n, hn);
setTabAt(tab, i, fwd);
advance = true;
}
}
}
}
}
}
标签:Node,ConcurrentHashMap,hash,HashMap,int,1.8,next,tab,null 来源: https://blog.csdn.net/weixin_43885975/article/details/118464075
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。