I have a Map which is to be modified by several threads concurrently.
There seem to be three different synchronized Map implementations in the Java API:
There is one critical feature to note about ConcurrentHashMap
other than concurrency feature it provides, which is fail-safe iterator. I have seen developers using ConcurrentHashMap
just because they want to edit the entryset - put/remove while iterating over it.
Collections.synchronizedMap(Map)
does not provide fail-safe iterator but it provides fail-fast iterator instead. fail-fast iterators uses snapshot of the size of map which can not be edited during iteration.