javajava.util.concurrentconcurrenthashmap

ConcurrentHashMap read, write and clear()


Suppose there are 3 reading threads and 1 writing thread. The writing thread calls map.clear(). What exception or effect will be caused by it?


Solution

  • The clear method will clear the whole map, i.e. deleting all entries.

    In view of concurrency, for reading actions, nothing changes. They will just read the value the map has at the point of time they access it. However for other threads that use writing actions the ConcurrentHashMap automatically waits for the clear operation to finish such that you do not experience such strange effects that come with non synchronized concurrency.


    Just carefully read through the official documentation, it explains all effects: ConcurrentHashMap Java-Doc