84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
e.hash == hash && key.equals(e.key)
The previous sentence explains that when there is thiskey的,你看看put方法,当value为nullin the table, an exception will occur:
key
put
value
null
public V put(K key, V value) { Segment s; if (value == null) throw new NullPointerException(); int hash = hash(key); int j = (hash >>> segmentShift) & segmentMask; if ((s = (Segment)UNSAFE.getObject // nonvolatile; recheck (segments, (j << SSHIFT) + SBASE)) == null) // in ensureSegment s = ensureSegment(j); return s.put(key, hash, value, false); }
This is a higher version of mine. It may be different from yours, but the value cannot be empty.So:
if (v != null) return v; return readValueUnderLock(e); // recheck
If it is not empty, it can be returned directly. If it is empty, it means that other threads are operating it. So I added it.
public V get(Object key) { Segment s; // manually integrate access methods to reduce overhead HashEntry[] tab; int h = hash(key); long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE; if ((s = (Segment)UNSAFE.getObjectVolatile(segments, u)) != null && (tab = s.table) != null) { for (HashEntry e = (HashEntry) UNSAFE.getObjectVolatile (tab, ((long)(((tab.length - 1) & h)) << TSHIFT) + TBASE); e != null; e = e.next) { K k; if ((k = e.key) == key || (e.hash == h && key.equals(k))) return e.value; } } return null; }
The current versionget方法把HashEntry e弄成UNSAFE.getObjectVolatile()获取,像是volatileis out
get
HashEntry e
UNSAFE.getObjectVolatile()
volatile
The poster can upgrade the JDK, I use it1.8找了一下,没发现这段代码在ConcurrentHashMap.
1.8
ConcurrentHashMap
The object is null when it is initialized. This null is not assigned specifically in the program.
The previous sentence explains that when there is this
key
的,你看看put
方法,当value
为null
in the table, an exception will occur:This is a higher version of mine. It may be different from yours, but the value cannot be empty.
So:
If it is not empty, it can be returned directly. If it is empty, it means that other threads are operating it. So I added it.
The current version
get
方法把HashEntry e
弄成UNSAFE.getObjectVolatile()
获取,像是volatile
is outThe poster can upgrade the JDK, I use it
1.8
找了一下,没发现这段代码在ConcurrentHashMap
.The object is null when it is initialized. This null is not assigned specifically in the program.