(十二)并发集合——ConcurrentHashMap

ConcurrentHashMap

Java8中的ConcurrentHashMap采用了链表+红黑树的方式。如果头节点是Node类型,则后面就是一个普通的链表;如果头节点是TreeNode类型,后面就是一颗红黑树。(TreeNode是Node的子类)

链表和红黑树之间可以相互转换:初始的时候是链表,当链表中的元素超过某个阈值时(为8),把链表转换成红黑树;反之,当红黑树中的元素个数小于某个阈值时,再转换为链表。

最大容量为2^30,默认容量为16

ConcurrentHashMap源码比较复杂,简单理解下初始化和put方法的大体流程:

public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
		implements ConcurrentMap<K,V>, Serializable {
	static class Node<K, V> implements Map.Entry<K, V> {
		final int hash;
		final K key;
		volatile V val;
		volatile Node<K, V> next;
	}
	private static final int MAXIMUM_CAPACITY = 1 << 30;
	public ConcurrentHashMap(int initialCapacity) {
		if (initialCapacity < 0)
			throw new IllegalArgumentException();
		//cap为Node数组长度,为2的整数次方
		int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
				MAXIMUM_CAPACITY :
				// 1.5*初始容量+1,向上取最接近的2的整数次方
				//保证数组长度为2的幂次方 好处是 xx%数组长度 = xx&(数组长度-1)
				tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
		//初始化时表示数组长度 或者 控制并发扩容的线程数
		this.sizeCtl = cap;
	}

	transient volatile Node<K, V>[] table;
	private transient volatile int sizeCtl;
	private static final int DEFAULT_CAPACITY = 16;
	static final int HASH_BITS = 0x7fffffff;
	static final int TREEIFY_THRESHOLD = 8;

	private final Node<K, V>[] initTable() {
		Node<K, V>[] tab;
		int sc;
		while ((tab = table) == null || tab.length == 0) {
			if ((sc = sizeCtl) < 0)
				Thread.yield();//sizeCtl<0时自旋等待
				//SIZECTL为sizeCtl的偏移量,CAS操作,将sizeCtl设置为-1,这样保证只有一个线程参与初始化
			else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
				try {
					if ((tab = table) == null || tab.length == 0) {
						int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
						//初始化
						@SuppressWarnings("unchecked")
						Node<K, V>[] nt = (Node<K, V>[]) new Node<?, ?>[n];
						table = tab = nt;
						//sizeCtl初始化完后不再表示数组长度,而是变成扩容阈值0.75n,finally块中设置
						sc = n - (n >>> 2);
					}
				} finally {
					sizeCtl = sc;
				}
				break;
			}
		}
		return tab;
	}
	private static final int tableSizeFor(int c) {
		int n = c - 1;
		n |= n >>> 1;
		n |= n >>> 2;
		n |= n >>> 4;
		n |= n >>> 8;
		n |= n >>> 16;
		return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
	}
	static final int spread(int h) {
		//将h无符号右移16位得到h',此时h'高16位为0,然后和h异或
		return (h ^ (h >>> 16)) & HASH_BITS;
	}

	public V put(K key, V value) {
		return putVal(key, value, false);
	}


	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;
		for (Node<K,V>[] tab = table;;) {
			Node<K,V> f; int n, i, fh;
			if (tab == null || (n = tab.length) == 0)
				tab = initTable();//如果table为空需要先初始化
			else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
				//第i个元素为空需要初始化,对第i个元素进行CAS操作
				if (casTabAt(tab, i, null, new Node<K,V>(hash, key, value, null)))
					break;
			}
			else if ((fh = f.hash) == MOVED)
				//改槽正在扩容,协助扩容
				tab = helpTransfer(tab, f);
			else {
				//放入元素
				V oldVal = null;
				//对table[i]元素加锁,也就是说table每个元素都作为一把锁
				synchronized (f) {
					//f=table[i]才继续,否则就重新循环
					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;
							}
						}
					}
				}
				//是链表的话,binCount变量会从1一直累加
				if (binCount != 0) {
					//如果binCount超出阈值,则转为红黑树
					if (binCount >= TREEIFY_THRESHOLD)
						treeifyBin(tab, i);
					if (oldVal != null)
						return oldVal;
					break;
				}
			}
		}
		//总元素个数+1
		addCount(1L, binCount);
		return null;
	}
	static final int MIN_TREEIFY_CAPACITY = 64;
	private final void treeifyBin(Node<K,V>[] tab, int index) {
		Node<K,V> b; int n, sc;
		if (tab != null) {
			if ((n = tab.length) < MIN_TREEIFY_CAPACITY)
				//数组长度<64,不转成红黑树,直接扩容
				tryPresize(n << 1);
			else if ((b = tabAt(tab, index)) != null && b.hash >= 0) {
				//链表转红黑树
				synchronized (b) {
					if (tabAt(tab, index) == b) {
						TreeNode<K,V> hd = null, tl = null;
						//遍历链表,构造红黑树
						for (Node<K,V> e = b; e != null; e = e.next) {
							TreeNode<K,V> p = new TreeNode<K,V>(e.hash, e.key, e.val, null, null);
							if ((p.prev = tl) == null)
								hd = p;
							else
								tl.next = p;
							tl = p;
						}
						setTabAt(tab, index, new TreeBin<K,V>(hd));
					}
				}
			}
		}
	}

	private final void tryPresize(int size) {
		//根据元素个数重新计算数组长度
		int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
				tableSizeFor(size + (size >>> 1) + 1);
		int sc;
		while ((sc = sizeCtl) >= 0) {
			Node<K,V>[] tab = table; int n;
			//如果table为空则先初始化
			if (tab == null || (n = tab.length) == 0) {
				n = (sc > c) ? sc : c;
				if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
					try {
						if (table == tab) {
							@SuppressWarnings("unchecked")
							Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
							table = nt;
							sc = n - (n >>> 2);//0.75n,下次扩容的阈值
						}
					} finally {
						sizeCtl = sc;
					}
				}
			}
			//如果sizeCtl>=新算出的长度 或者 超过数组最大长度,则不扩容
			else if (c <= sc || n >= MAXIMUM_CAPACITY)
				break;
			else if (tab == table) {
				int rs = resizeStamp(n);
				//多个线程进行并发扩容
				if (sc < 0) {
					Node<K,V>[] nt;
					if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
							sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
							transferIndex <= 0)
						//扩容结束
						break;
					if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))
						//协助扩容
						transfer(tab, nt);
				}
				else if (U.compareAndSwapInt(this, SIZECTL, sc,
						(rs << RESIZE_STAMP_SHIFT) + 2))
					//第一次扩容
					transfer(tab, null);
			}
		}
	}
	static final int NCPU = Runtime.getRuntime().availableProcessors();
	private static final int MIN_TRANSFER_STRIDE = 16;
	private transient volatile Node<K,V>[] nextTable;
	//表示整个数组扩容的进度
	private transient volatile int transferIndex;

	/**
	 * 迁移元素
	 * @param tab 旧数组
	 * @param nextTab 新数组
	 */
	private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
		//此方法可以被多线程调用,每个线程负责扩容一部分
		/**
		 * 1.先计算每个线程扩容的步长
		 * 2.如果新数组nextTab为空则初始化,并设置transferIndex初始为旧数组长度,从大到小扩容,每次减去stride个位置,
		 * 直到n<=0,扩容完成。[0,transferIndex]位置表示还没分配到线程扩容;[transferIndex,n]表示已经分配线程扩容,
		 * 状态为正在扩容或者扩容成功。
		 */
		int n = tab.length, stride;
		//计算步长,单核CPU下为n,只要一个线程,多核下保证步长最小值为16,扩容需要的线程数为 n/stride
		if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
			stride = MIN_TRANSFER_STRIDE;
		//初始化新的数组
		if (nextTab == null) {
			try {
				//扩容2倍,保证新数组长度仍是2的幂次方
				@SuppressWarnings("unchecked")
				Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n << 1];
				nextTab = nt;
			} catch (Throwable ex) {      // 处理内存溢出异常
				sizeCtl = Integer.MAX_VALUE;
				return;
			}
			nextTable = nextTab;
			//起始的transferIndex为旧的数组长度
			transferIndex = n;
		}
		int nextn = nextTab.length;
		//转发节点是为了解决 部分槽扩容完成,但是在get调用的时候还是调用的原来的hashmap的情况,此节点保存新的数组的引用
		//当调用get时,会通过此节点转发到新数组进行访问
		ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
		boolean advance = true;
		boolean finishing = false; // to ensure sweep before committing nextTab
		//i为遍历索引,bound为遍历的边界。如果成功拿到一个任务,i=nextIndex-1,bound=nextIndex=stride;
		//如果拿不到任务,i=0,bound=0;
		for (int i = 0, bound = 0;;) {
			Node<K,V> f; int fh;
			//只要有一个if分支执行就不再死循环,用while的目的是确保CAS操作能成功(自旋),拿到一个stride迁移任务
			while (advance) {
				int nextIndex, nextBound;
				//对数组遍历
				if (--i >= bound || finishing)
					advance = false;
					//整个hashmap完成
				else if ((nextIndex = transferIndex) <= 0) {
					i = -1;
					advance = false;
				}
				//为当前线程分配一个stride,CAS成功,拿到一个stride迁移任务,不成功,继续while循环
				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
					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
				}
			}
			//tab[i]迁移完成,赋值一个ForwardingNode
			else if ((f = tabAt(tab, i)) == null)
				advance = casTabAt(tab, i, null, fwd);
				//tab[i]的位置已经在迁移中
			else if ((fh = f.hash) == MOVED)
				advance = true; // already processed
				//对tab[i]进行迁移,tab[i]可能是一个链表或红黑树
			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);
							}
							/**
							 * 因为数组长度为2的幂次方,所以 hashCode%tab.length =  hashCode & (tab.length-1)。
							 * 说明原来处于第i个位置的元素在新的数组中也一定处于第i个位置或者i+n个位置
							 * 例如:数据长度为8,扩容后为16.
							 * 设hashCode=7; 7%8=7;7%16=7;位置不变
							 * 设hashCode=16; 16%8=0;16%16=0;位置不变
							 * 设hashCode=25; 25%8=1;25%16=9;后移8个位置
							 * 设hashCode=36; 36%8=4;36%16=4;位置不变
							 * 所以把tab[i]的链表或红黑树分为两部分,一部分到nextTab[i]位置,另一部分到nextTab[i+n]位置
							 * 然后把tab[i]的位置指向一个ForwardingNode节点
							 *
							 */
							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;
						}
					}
				}
			}
		}
	}
	static final <K,V> void setTabAt(Node<K,V>[] tab, int i, Node<K,V> v) {
		U.putObjectVolatile(tab, ((long)i << ASHIFT) + ABASE, v);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_32076957/article/details/128447984