Search code examples
javamultithreadingconcurrenthashmap

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


can someone explain the interleaving of two threads such that one of the thread gets null when two threads call putIfAbsent of ConcurrentHashMap in Java?


Solution

  • From the javadoc of putIfAbsent:

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

    So the first thread attempting to put the value in the map will always have null returned to it.