Make delicious recipes!

ConcurrentHashMap

A hash-map which supports full concurrency for read operations and adjustable expected concurrency for write operations.

It takes an integer argument concurrencyLevel in the constructor which specifies how many concurrent write operations are expected to occur for the hash-map. The hash-map then tries to allow that many write threads to execute concurrently by internally partitioning the table into that many partitions assuming that a single write thread will use a different partition. However, that is not guaranteed because all threads may want to update a single partition at the same time. Hence this write concurrency is a best effort implementation.





Difference between

ConcurrentHashMap and Collections.synchronizedMap (new HashMap ())

CHM is concurrent while other one is synchronized.

Synchronized means that it blocks other threads if a single thread is reading or writing.

Concurrent is a much more efficient version which uses ReadWriteLocks to allow concurrent reads and and adjustable concurrent writes. Also, it uses Atomic variables (explained 3-4 pages below) which use Compare-And-Swap technique which is much more efficient than blocking or synchronization.

Similarly, all concurrent data structures in the concurrent package are much more efficient than their synchronous counterparts.


Thread-Safe Collections

The key difference between the historical collection classes and the new implementations within the Collections Framework is the new classes are not thread-safe. This was done such that if you don't need the synchronization, you don't use it, and everything works much faster. If, however, you are using a collection in a multithreaded environment, where multiple threads can modify the collection simultaneously, the modifications need to be synchronized. The Collections class provides for the the ability to wrap existing collections into synchronized ones with another set of six methods:

  • Collection synchronizedCollection(Collection collection)

  • List synchronizedList(List list)

  • Map synchronizedMap(Map map)

  • Set synchronizedSet(Set set)

  • SortedMap synchronizedSortedMap(SortedMap map)

  • SortedSet synchronizedSortedSet(SortedSet set)

Unlike when making a collection read-only, you synchronize the collection immediately after creating it. You also must make sure you do not retain a reference to the original collection, or else you can access the collection unsynchronized. The simplest way to make sure you don't retain a reference is to never create one:

Set set = Collection.synchronizedSet(new HashSet());

Making a collection unmodifiable also makes a collection thread-safe, as the collection can't be modified. This avoids the synchronization overhead.


Implementation of thread-safe collections

Collections.synchronizedList() are implemented much like the Collections.unmodifiableList() methods. When synchronizedList() is called, it creates a wrapper class. All functions of this wrapper class are synchronized and each function simply calls the original collection’s function.

Thus, the wrapper class implements the corresponding collection’s interface and makes it synchronized too.

However, this means that the resulting collection is very heavily synchronized and can become a performance bottleneck.




Like us on Facebook to remain in touch
with the latest in technology and tutorials!


Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Name:
Email: (Your email is not shared with anybody)
Comment:

Facebook comments:

Site Owner: Sachin Goyal