ConCurrentHashMap的底层是:散列表+红黑树,与HashMap是一样的。

//在源码中我们发现了volatile关键字 //但是通过JVM中的第一篇我们知道volatile可以实现原子性,但是只能限定于某些特定,例如如果本身方法 //是一个非原子性的,例如I++这种,就会依然不安全 transient volatile Node<K,V>[] table; //继续往下面看,发现其实Put方法也就是一个掩护,其实真正的还是putVal方法 //而putVal方法其实是选择了加锁(casTabAt,tabAt) public V put(K key, V value) { return putVal(key, value, false); } /** Implementation for put and putIfAbsent */ 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; } //然后继续看tabAt和CasAt中,可以发现有一个"U",点进去就会发现 private static final sun.misc.Unsafe U; //Unsafe,再往下面看就会发现这个东西其实不是Java写的。Unsafe类提供了硬件级别的原子操作 //也就是说这个对象可以直接对内存动刀子。所以这个对象的getObjectVolatile方法就一定可以获取到最新的table。 //所以无惧高并发带来的内存一致性错误的烦恼。
2.它检索是不加锁的而且能获取到最新的值
//会发现源码中没有一处加了锁 public V get(Object key) { Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek; int h = spread(key.hashCode()); //计算hash if ((tab = table) != null && (n = tab.length) > 0 && (e = tabAt(tab, (n - 1) & h)) != null) {//读取首节点的Node元素 if ((eh = e.hash) == h) { //如果该节点就是首节点就返回 if ((ek = e.key) == key || (ek != null && key.equals(ek))) return e.val; } //hash值为负值表示正在扩容,这个时候查的是ForwardingNode的find方法来定位到nextTable来 //eh=-1,说明该节点是一个ForwardingNode,正在迁移,此时调用ForwardingNode的find方法去nextTable里找。 //eh=-2,说明该节点是一个TreeBin,此时调用TreeBin的find方法遍历红黑树,由于红黑树有可能正在旋转变色,所以find里会有读写锁。 //eh>=0,说明该节点下挂的是一个链表,直接遍历该链表即可。 else if (eh < 0) return (p = e.find(h, key)) != null ? p.val : null; while ((e = e.next) != null) {//既不是首节点也不是ForwardingNode,那就往下遍历 if (e.hash == h && ((ek = e.key) == key || (ek != null && key.equals(ek)))) return e.val; } } return null; } //会发现没有一点锁那么是如果做到不会读到脏数据的呢? //这时候返过来思考,你会怎么设计?在加上前面的那个JVM关键字 //应该明白了把 //这时候应该去看看它的数组是不是加了volatile static class Node<K,V> implements Map.Entry<K,V> { final int hash; final K key; volatile V val; volatile Node<K,V> next; ........ } //果然加了,因为这边的查询方法其实本身是原子性的所以volatile保证了原子性。
别的好像问题不大,参考HashMap就行
看完了整个 HashMap 和 ConcurrentHashMap 在 1.7 和 1.8 中不同的实现方式相信大家对他们的理解应该会更加到位。
其实这块也是面试的重点内容,通常的套路是:
这一串问题相信大家仔细看完都能怼回面试官。
原文:https://www.cnblogs.com/SmartCat994/p/13189673.html