Wednesday, September 16, 2015



This is only for my reference gathered from

http://crunchify.com/hashmap-vs-concurrenthashmap-vs-synchronizedmap-how-a-hashmap-can-be-synchronized-in-java/

  • What’s the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?
  • What’s the difference between ConcurrentHashMap and Collections.synchronizedMap(Map) in term of performance?
  • ConcurrentHashMap vs Collections.synchronizedMap()
  • Popular HashMap and ConcurrentHashMap interview questions
In this tutorial we will go over all above queries and reason why and how we could Synchronize Hashmap?

Why?

The Map object is an associative containers that store elements, formed by a combination of a uniquely identify key and a mapped value. If you have very highly concurrent application in which you may want to modify or read key value in different threads then it’s ideal to use Concurrent Hashmap. Best example is Producer Consumer which handles concurrent read/write.
So what does the thread-safe Map means? If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally to avoid an inconsistent view of the contents.

How?

There are two ways we could synchronized HashMap
  1. Java Collections synchronizedMap() method
  2. Use ConcurrentHashMap

ConcurrentHashMap

  • You should use ConcurrentHashMap when you need very high concurrency in your project.
  • It is thread safe without synchronizing the whole map.
  • Reads can happen very fast while write is done with a lock.
  • There is no locking at the object level.
  • The locking is at a much finer granularity at a hashmap bucket level.
  • ConcurrentHashMap doesn’t throw a ConcurrentModificationException if one thread tries to modify it while another is iterating over it.
  • ConcurrentHashMap uses multitude of locks.

SynchronizedHashMap

  • Synchronization at Object level.
  • Every read/write operation needs to acquire lock.
  • Locking the entire collection is a performance overhead.
  • This essentially gives access to only one thread to the entire map & blocks all the other threads.
  • It may cause contention.
  • SynchronizedHashMap returns Iterator, which fails-fast on concurrent modification.

Now let’s take a look at code

  1. Create class CrunchifyConcurrentHashMapVsSynchronizedHashMap.java
  2. Create object for each HashTable, SynchronizedMap and CrunchifyConcurrentHashMap
  3. Add and retrieve 500k entries from Map
  4. Measure start and end time and display time in milliseconds
  5. We will use ExecutorService to run 5 threads in parallel










Property      ConcurrentHashMap HashMap
1.  Thread -Safe :       ConcurrentHashMap is thread-safe that is the code can be accessed by single thread at a time .            while HashMap is not thread-safe .
2. Synchronization Method ConcurrentHashMap synchronizes or locks on the certain portion of the Map . To optimize
   the performance of ConcurrentHashMap , Map is divided into different partitions depending
   upon the Concurrency level . So that we do not need to synchronize the whole Map Object.
HashMap can be synchronized by using   
    synchronizedMap(HashMap)  method .  By using this 

    method we get a HashMap object which is equivalent
    to the HashTable object . So every modification  is performed   
    on  Map is locked on Map object.
3.  Null Key ConcurrentHashMap does not allow NULL values . So the key can not be null in
     ConcurrentHashMap 
HashMap there can only be one null key .
4.  Performance In multiple threaded environment HashMap is usually faster than ConcurrentHashMap . As   
     only single thread can access the certain portion of the Map and thus reducing the  performance .

 in HashMap any number of threads can     access the code at the same time .

No comments:

Post a Comment