JDK-ConcurrentHashMap Analysis
I'm curious that ConcurrentHashMap is a thread-safe map, so I tried to read the JDK source code.
ConcurrentHashMap is divided into segments. Then, each time you want to put or get an element, find the segment and execute put or get. For thread security, a lock is added to the put method.
Check the segment attribute.
final Segment
[] segments;
Segmen contains the following attributes:
transient volatile HashEntry
[] table;
Check out HashEntry.
static final class HashEntry
{ final K key; final int hash; volatile V value; final HashEntry
next; HashEntry(K key, int hash, HashEntry
next, V value) { this.key = key; this.hash = hash; this.next = next; this.value = value; }@SuppressWarnings("unchecked")static final
HashEntry
[] newArray(int i) { return new HashEntry[i];} }
Isn't that the key-Value Pair stored...
Let's take a look at the put Method of ConcurrentHashMap.
public V put(K key, V value) { if (value == null) throw new NullPointerException(); int hash = hash(key.hashCode()); return segmentFor(hash).put(key, hash, value, false); }Find the segment through the segmentFor method, and then hand it to the segment for the put operation.
Let's look at the put Method in the Segment class.
V put(K key, int hash, V value, boolean onlyIfAbsent) { lock(); try { int c = count; if (c++ > threshold) // ensure capacity rehash(); HashEntry
[] tab = table; int index = hash & (tab.length - 1); HashEntry
first = tab[index]; HashEntry
e = first; while (e != null && (e.hash != hash || !key.equals(e.key))) e = e.next; V oldValue; if (e != null) { oldValue = e.value; if (!onlyIfAbsent) e.value = value; } else { oldValue = null; ++modCount; tab[index] = new HashEntry
(key, hash, first, value); count = c; // write-volatile } return oldValue; } finally { unlock(); } }
The lock is first added, and then the array subscript of the object is found through the hash value. Then, cycle the e nodes with the same hash value. If the key is the same, replace the value. Otherwise, insert the node. Then manually release the lock.
This ensures thread security, which is much more efficient than hashtable. Because hashtable synchronizes all its methods.
ConcurrentHashMap has a size method, which is worth looking.
public int size() { final Segment
[] segments = this.segments; long sum = 0; long check = 0; int[] mc = new int[segments.length]; // Try a few times to get accurate count. On failure due to // continuous async changes in table, resort to locking. for (int k = 0; k < RETRIES_BEFORE_LOCK; ++k) { check = 0; sum = 0; int mcsum = 0; for (int i = 0; i < segments.length; ++i) { sum += segments[i].count; mcsum += mc[i] = segments[i].modCount; } if (mcsum != 0) { for (int i = 0; i < segments.length; ++i) { check += segments[i].count; if (mc[i] != segments[i].modCount) { check = -1; // force retry break; } } } if (check == sum) break; } if (check != sum) { // Resort to locking all segments sum = 0; for (int i = 0; i < segments.length; ++i) segments[i].lock(); for (int i = 0; i < segments.length; ++i) sum += segments[i].count; for (int i = 0; i < segments.length; ++i) segments[i].unlock(); } if (sum > Integer.MAX_VALUE) return Integer.MAX_VALUE; else return (int)sum; }