本篇博客学习于https://www.cnblogs.com/zerotomax/p/8687425.html
?/currentHashMap集合同样是位于java.util包路径下面的
package java.util.concurrent;
//继承的包以及实现的接口分别都有哪些
public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
implements ConcurrentMap<K,V>, Serializable {
//允许的最大容量值
private static final int MAXIMUM_CAPACITY = 1 << 30;
//默认的容量-注释上面指明必须是2的倍数Must be a power of 2
private static final int DEFAULT_CAPACITY = 16;
//跟hashMap集合中是一样的,负载因子为0.75浮点数
private static final float LOAD_FACTOR = 0.75f;
//链表中允许挂载的节点数为8
static final int TREEIFY_THRESHOLD = 8;
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 正常
//Node对象都是与HashMap一样的底层数据结构
static class Node<K,V> implements Map.Entry<K,V> {
final int hash; //hash值
final K key; //key
volatile V val; //value
volatile Node<K,V> next; //下个节点Node对象
Node(int hash, K key, V val, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.val = val;
this.next = next;
}
//原子性获取索引中的值
static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) {
return (Node<K,V>)U.getObjectVolatile(tab, ((long)i << ASHIFT) + ABASE);
}
//cas设置值
static final <K,V> boolean casTabAt(Node<K,V>[] tab, int i,
Node<K,V> c, Node<K,V> v) {
return U.compareAndSwapObject(tab, ((long)i << ASHIFT) + ABASE, c, v);
}
//原子性设置值
static final <K,V> void setTabAt(Node<K,V>[] tab, int i, Node<K,V> v) {
U.putObjectVolatile(tab, ((long)i << ASHIFT) + ABASE, v);
}
//数组复制之前的数组
transient volatile Node<K,V>[] table;
//备用的数组
private transient volatile Node<K,V>[] nextTable;
//初始化与扩容的标识数据
private transient volatile int sizeCtl;
//无参构造没有做任何事情
public ConcurrentHashMap() {
}
主要是看添加元素的操作
//put方法主要是调用putVal方法
public V put(K key, V value) {
return putVal(key, value, false);
}
//该集合中不允许存储key为null与value为null的值,否则会抛出空指针异常
//首先判断是否初始化,没有就调用initTable方法初始化表
//判断查询出的索引处是否为null,为null就直接插入
//如果f.hash为-1数组复制时就将当前线程参与数组的复制
//不是就对数组中的元素加锁,遍历链表查询符合就覆盖,没有就添加到连尾
//如果为树结构就直接调用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;
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(tab, i = (n - 1) & hash)) == null) {
if (casTabAt(tab, i, null,
new Node<K,V>(hash, key, value, null)))
break; // no lock when adding to empty bin
}
else if ((fh = f.hash) == MOVED)
tab = helpTransfer(tab, f);
else {
V oldVal = null;
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;
}
}
}
addCount(1L, binCount);
return null;
}
get方法 读操作没有对元素加锁
//代码逻辑与hashMap中的get方法实现一样的
public V get(Object key) {
Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
int h = spread(key.hashCode());
if ((tab = table) != null && (n = tab.length) > 0 &&
(e = tabAt(tab, (n - 1) & h)) != null) {
if ((eh = e.hash) == h) {
if ((ek = e.key) == key || (ek != null && key.equals(ek)))
return e.val;
}
else if (eh < 0)
return (p = e.find(h, key)) != null ? p.val : null;
while ((e = e.next) != null) {
if (e.hash == h &&
((ek = e.key) == key || (ek != null && key.equals(ek))))
return e.val;
}
}
return null;
}
initTable初始化的方法
//根据sizeCtl的值判断是否在扩容操作,别的线程操作数组,是就停止执行
//sc大于0说明数组有记录,等于0是默认的,然后sizeCtl为sc的3/4值
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(); // lost initialization race; just spin
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;
sc = n - (n >>> 2);
}
} finally {
sizeCtl = sc;
}
break;
}
}
return tab;
}