I was reading the implementation of Java11 Concurrent Hashmap. Below is the snipped of 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;
}
I also read the remove(key)
implementation and noticed that upon removal of a key
, the corresponding node of the key is not referenced in the hashtable and removed.
Now what will happen to the concurrent get(key)
call?
If
remove(k1)
is called and completed by another thread just beforeget(k1)
was about to return thee.val
, the stale value(non-null value) of key would be returned. Hence ignoring the most recently completed update operation.
Is the above statement correct?