【Java 源码技术】--Difference between HashMap and ConcurrentHashMap(165)
2017-04-07 23:04
477 查看
1) The first significant difference between HashMap and ConcurrentHashMap is that later is thread-safe and can be used in a concurrent environment without external synchronization. Though it doesn’t provide the same level of synchronization as achieved by using Hashtable but it’s enough for the most practical purpose.
2)You can make HashMap synchronized by wrapping it on Collections.synchornizedMap(HashMap) which will return a collection which is almost equivalent to Hashtable, where every modification operation on Map is locked on Map object while in case of ConcurrentHashMap, thread-safety is achieved by dividing whole Map into different partition based upon Concurrency level and only locking particular portion instead of locking the whole Map.
Difference between ConcurrentHashMap and HashMap in Java Collection
3) ConcurrentHashMap is more scalable and performs better than Synchronized HashMap in the multi-threaded environment while in Single threaded environment both HashMap and ConcurrentHashMap gives comparable performance, where HashMap only slightly better.
In Summary Main difference between ConcurrentHashMap and HashMap in Java Collection turns out to be thread-safety, Scalability, and Synchronization. ConcurrentHashMap is a better choice than synchronized HashMap if you are using them as cache, which is the most popular use case of a Map in Java application. ConcurrentHashMap is more scalable and outperforms when a number of reader threads outnumber the number of writer threads.
2)You can make HashMap synchronized by wrapping it on Collections.synchornizedMap(HashMap) which will return a collection which is almost equivalent to Hashtable, where every modification operation on Map is locked on Map object while in case of ConcurrentHashMap, thread-safety is achieved by dividing whole Map into different partition based upon Concurrency level and only locking particular portion instead of locking the whole Map.
Difference between ConcurrentHashMap and HashMap in Java Collection
3) ConcurrentHashMap is more scalable and performs better than Synchronized HashMap in the multi-threaded environment while in Single threaded environment both HashMap and ConcurrentHashMap gives comparable performance, where HashMap only slightly better.
In Summary Main difference between ConcurrentHashMap and HashMap in Java Collection turns out to be thread-safety, Scalability, and Synchronization. ConcurrentHashMap is a better choice than synchronized HashMap if you are using them as cache, which is the most popular use case of a Map in Java application. ConcurrentHashMap is more scalable and outperforms when a number of reader threads outnumber the number of writer threads.
相关文章推荐
- Difference between ConcurrentHashMap and Collections.synchronizedMap and Hashtable in Java
- Java - Difference between ConcurrentHashMap, Hashtable and Synchronized Map
- Java - Difference between HashMap and HashSet in Java
- Difference between HashMap, LinkedHashMap and TreeMap in Java
- Java - What is difference between HashMap and Hashtable?
- What is difference between HashMap and Hashtable in Java?
- Difference between HashMap and Hashtable in Java
- 【JAVA秒会技术之ConcurrentHashMap】JDK1.7与JDK1.8源码区别
- Difference between HashSet and HashMap in Java
- Java Main Differences between HashMap and HashTable
- Difference between Set, List and Map in Java - Interview question
- The Differences Between C++ and Java
- Difference between java.rmi.Naming and java.rmi.registry.LocateRegistry
- Differences or similarities between Java and C++
- differences between “java -server” and “java -client”
- Difference between DOM and SAX Parsers in Java
- Difference between List and Set in Java Collection
- Difference between Serializable and Externalizable in Java Serialization
- the difference between final and static in java
- Difference between ArrayList and Vector in Java