Here is a good article to tell the different between different Map implementation.
util.concurrent(which will also appear in the
java.util.concurrentpackage in JDK 1.5) is a thread-safe implementation of
Mapthat offers far better concurrency than
synchronizedMap. Multiple reads can almost always execute concurrently, simultaneous reads and writes can usually execute concurrently, and multiple simultaneous writes can often execute concurrently. (The related
ConcurrentReaderHashMapclass offers similar multiple-reader concurrency, but allows only a single active writer.)
ConcurrentHashMapis designed to optimize retrieval operations; in fact, successful
get()operations usually succeed with no locking at all. Achieving thread-safety without locking is tricky and requires a deep understanding of the details of the Java Memory Model. The
ConcurrentHashMapimplementation, along with the rest of
util.concurrent, has been extensively peer-reviewed by concurrency experts for correctness and thread safety. We will look at the implementation details of
ConcurrentHashMapin next month’s article.
ConcurrentHashMapachieves higher concurrency by slightly relaxing the promises it makes to callers. A retrieval operation will return the value inserted by the most recent completed insert operation, and may also return a value added by an insertion operation that is concurrently in progress (but in no case will it return a nonsense result).
ConcurrentHashMap.iterator()will return each element once at most and will not ever throw
ConcurrentModificationException, but may or may not reflect insertions or removals that occurred since the iterator was constructed. No table-wide locking is needed (or even possible) to provide thread-safety when iterating the collection.
ConcurrentHashMapmay be used as a replacement for
Hashtablein any application that does not rely on the ability to lock the entire table to prevent updates.
These compromises enable
ConcurrentHashMapto provide far superior scalability over
Hashtable, without compromising its effectiveness in a wide variety of common-use cases, such as shared caches.
Also, if possible when you have a multithread program, and when you need to reload cache, it is good to construct a new cache, and then swap the instance. E.g.
Popularity: 2% [?]