Difference between ConcurrentHashMap and Collections.synchronizedMap and Hashtable in Java

Addthis

Sunday, April 24, 2011

Difference between ConcurrentHashMap and Collections.synchronizedMap and Hashtable in Java

Collections classes are heart of java API though I feel using them judiciously is an art. Its my personal experience where I haveimproved performance by using ArrayList where legacy codes are unnecessarily used Vector etc.
JDK 1.5 introduces some good concurrent collections which is highly efficient for high volume, low latency system electronictrading systems In general those are backbone for Concurrent fast access of stored data. In thistutorial we will look on ConcurrentHashMap, Hashtable and HashMap and Collections.synchronizedMap and see difference between ConcurrentHashMap and Hashtable and synchronizedMap.


This Java Collection tutorial is in continuation of myarticle How HashMap works in Javaand difference between HashMap and Hashtable in Java if you haven’t read already you may find some useful information based on my experience in Java Collections.

Why we need ConcurrentHashMap and CopyOnWriteArrayList

The synchronized collections classes, Hashtable and Vector, and the synchronized wrapper classes,Collections.synchronizedMap and Collections.synchronizedList, provide a basic conditionally thread-safe implementation of Map and List. However, several factors make them unsuitable for use in highly concurrent applicationsfor example their singlecollection-wide lock is an impediment to scalability and it often becomes necessary to lock a collection for a considerable time during iteration to preventConcurrentModificationException.

ConcurrentHashMap and CopyOnWriteArrayList implementations provide much higher concurrency while preserving thread safety, with some minor compromises in their promises to callers. ConcurrentHashMap and CopyOnWriteArrayList are not necessarily useful everywhere you might use HashMap or ArrayList, but are designed to optimize specific common situations. Many concurrent applications will benefit from their use.

Difference between ConcurrentHashMap and Hashtable

difference between concurrentHashMap, hashtable and Synchronized Map
So what is the difference between Hashtableand ConcurrentHashMap , both can be used in multithreaded environment but once the size of Hashtablebecomes considerable large performance degrade because for iteration it has to be locked for longer duration.

Since ConcurrentHashMap introduced concept of segmentation , how large it becomes only certain part of it get locked to provide thread safety so many other readers can still access map without waiting for iteration to complete.

In Summary ConcurrentHashMap only locked certain portion of Map while Hashtable lock full map while doing iteration.

Difference between ConcurrentHashMap and Collections.synchronizedMap

ConcurrentHashMap is designed for concurrency and improve performance while HashMap which is non synchronized by nature can be synchronized by applying a wrapper using Collections.synchronizedMap. Here are some of commondifferences between ConcurrentHashMap and Collections.synchronizedMap

ConcurrentHashMap do not allow null keys or null values while HashMap allows null keys.


Read more: http://javarevisited.blogspot.com/2011/04/difference-between-concurrenthashmap.html#ixzz2kmxX8lF9
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值