Difference between HashMap and ConcurrentHashMap

HashMap and Concurrent HashMap both are very important concept in java. So first let us discuss about HashMap and Concurrent HashMap then we will cover the differences between these two.

Concurrent HashMap

  1. The underlying data structure of ConcurrentHashMap is HashTable.
  2. It allows concurrent read and Thread safe update operations.
  3. To perform read operation thread won’t require any lock, But to perform update operation thread requires lock but this lock is only on particular part of Map (Bucket level lock).
  4. Initially Map is divided into 16 buckets, this 16 buckets are known as concurrency level.
  5. In this locking mechanism is on bucket level or segment level locking.
  6. In HashTable, Because of it is synchronized so, to perform any operation thread locks the whole map, But in case of ConcurrentHashMap, Thread does not lock whole map. It locks only the bucket.
  7. Any number of threads are allow to perform read operation in concurrentHashMap, But to perform update operation 16 threads are allowed concurrently.
  8. Default concurrency level of ConcurrentHashMap is 16.

Constructors of ConcurrentHashMap

  • ConcurrentHashMap chm = new ConcurrentHashMap();
  • ConcurrentHashMap chm = new ConcurrentHashMap(int initialCapacity);
  • ConcurrentHashMap chm = new ConcurrentHashMap(int initialCapacity, float fillRatio);
  • ConcurrentHashMap chm = new ConcurrentHashMap(int initialCapacity, float fillRatio, int concurrencyLevel);
  • ConcurrentHashMap chm = new ConcurrentHashMap(Map m);
				
					package concurrentHashMapExample;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class CHM extends Thread{
    static ConcurrentHashMap<Integer, String> m = new ConcurrentHashMap<>();
    public void run(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("child thread updating the map ");
        m.put(103, "rahul");
    }
    public static void main(String[] args) {
        m.put(101, "satish");
        m.put(102, "manish");
        CHM c = new CHM();
        // this is child thread
        c.start();
        Set<Integer> s = m.keySet();
        Iterator<Integer> itr = s.iterator();
        // main thread iterating the map
        while (itr.hasNext()) {
            Integer i = (Integer)itr.next();
            System.out.println("main thread iterating the map and current entry is : "+ i + " - "+ m.get(i));
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(m);
    }
}

OUTPUT :

main thread iterating the map and current entry is : 101 - satish
child thread updating the map
main thread iterating the map and current entry is : 102 - manish
main thread iterating the map and current entry is : 103 - rahul
{101=satish, 102=manish, 103=rahul}
				
			

Output:

				
					
main thread iterating the map and current entry is : 101 - satish
child thread updating the map
main thread iterating the map and current entry is : 102 - manish
main thread iterating the map and current entry is : 103 - rahul
{101=satish, 102=manish, 103=rahul}
				
			
Scroll to Top