HashMap vs. ConcurrentHashMap vs. SynchronizedMap - How A HashMap Can Be Synchronized in Java - Crunchify
HashMap vs. ConcurrentHashMap vs. SynchronizedMap - How A HashMap Can Be Synchronized in Java - Crunchify
ADVERTISEMENT
Blog Java and J2EE Tutorials HashMap Vs. ConcurrentHashMap Vs. SynchronizedMap – How a HashMa ...
HashMap is a very powerful data structure in Java. We use it everyday and almost in all
applications. There are quite a few examples which I have written before on How to
Implement Threadsafe cache, How to convert Hashmap to Arraylist?
We used Hashmap in both above examples but those are pretty simple use cases of
Hashmap. HashMap is a non-synchronized collection class.
https://crunchify.com/hashmap-vs-concurrenthashmap-vs-synchronizedmap-how-a-hashmap-can-be-synchronized-in-java/ 1/9
17/08/2020 HashMap Vs. ConcurrentHashMap Vs. SynchronizedMap - How a HashMap can be Synchronized in Java • Crunchify
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
. Use ConcurrentHashMap
//Hashtable
Map<String, String> normalMap = new Hashtable<String, String>();
//synchronizedMap
synchronizedHashMap = Collections.synchronizedMap(new HashMap<String, String>());
//ConcurrentHashMap
concurrentHashMap = new ConcurrentHashMap<String, String>();
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.
https://crunchify.com/hashmap-vs-concurrenthashmap-vs-synchronizedmap-how-a-hashmap-can-be-synchronized-in-java/ 2/9
17/08/2020 HashMap Vs. ConcurrentHashMap Vs. SynchronizedMap - How a HashMap can be Synchronized in Java • Crunchify
package crunchify.com.tutorials;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* @author Crunchify.com
*
*/
https://crunchify.com/hashmap-vs-concurrenthashmap-vs-synchronizedmap-how-a-hashmap-can-be-synchronized-in-java/ 3/9
17/08/2020 HashMap Vs. ConcurrentHashMap Vs. SynchronizedMap - How a HashMap can be Synchronized in Java • Crunchify
System.out.println("Test started for: " + crunchifyThreads.getClass());
long averageTime = 0;
for (int i = 0; i < 5; i++) {
// Put value
crunchifyThreads.put(String.valueOf(crunchifyRandomNumber
}
}
});
}
// Blocks until all tasks have completed execution after a shutdown reque
// interrupted, whichever happens first.
crunchifyExServer.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
And hence, you need to first shutdown the serviceExecutor and then block and wait for
threads to finish.
https://crunchify.com/hashmap-vs-concurrenthashmap-vs-synchronizedmap-how-a-hashmap-can-be-synchronized-in-java/ 4/9
17/08/2020 HashMap Vs. ConcurrentHashMap Vs. SynchronizedMap - How a HashMap can be Synchronized in Java • Crunchify
500K entried added/retrieved in 2032 ms
500K entried added/retrieved in 1621 ms
500K entried added/retrieved in 1833 ms
500K entried added/retrieved in 2048 ms
For class java.util.Collections$SynchronizedMap the average time is 1891 ms
Share & leave us some comments on what you think about this topic or if you like to add
something.
Share:
Collection
. How to use net.jodah.ExpiringMap Maven Java Utility to Remove Expired Objects from
I WANT TO...
Optimize WordPress Plugins we use Get FREE Domain & Hosting Learn SEO
the largest free blogging & technical resource site for beginners. Love SEO,
SaaS, #webperf, WordPress, Java. With over 16 millions+ pageviews/month, Crunchify has
https://crunchify.com/hashmap-vs-concurrenthashmap-vs-synchronizedmap-how-a-hashmap-can-be-synchronized-in-java/ 5/9
17/08/2020 HashMap Vs. ConcurrentHashMap Vs. SynchronizedMap - How a HashMap can be Synchronized in Java • Crunchify
changed the life of over thousands of individual around the globe teaching Java & Web Tech for
Subscribe To Newsletter…
Get Early Access To New Articles, Plugins, Discount Codes And Brief Updates About What's
New With Crunchify! Join Over 16 Million Monthly Readers...
SEARCH
https://crunchify.com/hashmap-vs-concurrenthashmap-vs-synchronizedmap-how-a-hashmap-can-be-synchronized-in-java/ 6/9
17/08/2020 HashMap Vs. ConcurrentHashMap Vs. SynchronizedMap - How a HashMap can be Synchronized in Java • Crunchify
Java Reflection
https://crunchify.com/hashmap-vs-concurrenthashmap-vs-synchronizedmap-how-a-hashmap-can-be-synchronized-in-java/ 7/9
17/08/2020 HashMap Vs. ConcurrentHashMap Vs. SynchronizedMap - How a HashMap can be Synchronized in Java • Crunchify
WordPress Optimization and Tutorials Eclipse IDE Tutorials How to create 1st Spring Boot HelloWorld Application
Genesis WP Android Dev Tutorials in IntelliJ IDEA with few simple steps?
Blogging Apache Tomcat Tutorials How to use Spring Framework StopWatch() to Log
Maven
https://crunchify.com/hashmap-vs-concurrenthashmap-vs-synchronizedmap-how-a-hashmap-can-be-synchronized-in-java/ 8/9
17/08/2020 HashMap Vs. ConcurrentHashMap Vs. SynchronizedMap - How a HashMap can be Synchronized in Java • Crunchify
2020 Crunchify, LLC. Hosted at Kinsta • Built on Genesis Themes.
DCMA Disclaimer and Privacy Policy.
Noticed a bug? Report it here.
https://crunchify.com/hashmap-vs-concurrenthashmap-vs-synchronizedmap-how-a-hashmap-can-be-synchronized-in-java/ 9/9