交错两个线程,使得其中一个线程在调用 concurrentHashMap 的 putIfAbsent 时变为 null

Interleaving two threads such that one of the thread gets null when called putIfAbsent of concurrentHashMap

有人可以解释两个线程的交错,当两个线程在 Java 中调用 ConcurrentHashMapputIfAbsent 时,其中一个线程变为空吗?

来自 putIfAbsent 的 javadoc:

returns:
the previous value associated with the specified key, or null if there was no mapping for the key.

因此,第一个尝试将值放入映射中的线程总是会返回 null