Search code examples
concurrencyconcurrenthashmapthread-synchronization

Concurrent Hash Map get and put overlapping


I have read that get method is fully concurrent in ConcurrentHashMap(Jdk 7 ) and so it can overlap with all update operations. What will happen if two threads run put(Key,V) and Get (Key) concurrently if Key is not already present?


Solution

  • According to JDK7 Javadoc:

    Retrievals reflect the results of the most recently completed update operations holding upon their onset.

    This means get() will return null if it was invoked before put() completed.

    The short answer to your question is: put() will return null.