Collections classes are heart of java API though I feel using them judiuously is an art.its my personal experience where I have improved performance by using ArrayList where legacy codes are unnecesarily used Vector etc.
JDK 1.5 introduce some good concurrent collections which is highly efficient for high volume , low latency system.
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 applications -- their single collection-wide lock is an impediment to scalability and it often becomes necessary to lock a collection for a considerable time during iteration to prevent ConcurrentModificationExceptions.
The 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.
So what is the difference between hashtable and ConcurrentHashMap , both can be used in multithreaded environment but once the size of hashtable becomes considerable large performance degrade because for iteration it has to be locked for longer duration.
Since ConcurrentHashMap indroduced 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.
As always comments , suggestions are welcome.
JDK 1.5 introduce some good concurrent collections which is highly efficient for high volume , low latency system.
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 applications -- their single collection-wide lock is an impediment to scalability and it often becomes necessary to lock a collection for a considerable time during iteration to prevent ConcurrentModificationExceptions.
The 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.
So what is the difference between hashtable and ConcurrentHashMap , both can be used in multithreaded environment but once the size of hashtable becomes considerable large performance degrade because for iteration it has to be locked for longer duration.
Since ConcurrentHashMap indroduced 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.
As always comments , suggestions are welcome.
13 comments:
good stuff man keep it up
Very nice article...
Thanks Anonymous.
Hi!
You said "ConcurrentHashMap and CopyOnWriteArrayList are not necessarily useful everywhere you might use HashMap or ArrayList". OK, fair enough, but why is that? These classes don't perform well with small size?
What makes that classes not fully interchangeable with their counterparts?
Thank you in advance
@Leandro: I wanna the same thing, too.
Nice article. Thanks for the heads-up.
hi @Leandro, Since hashMap is non synchronized it is the fastest available solution when synchronization is not required. Same is true with Arraylist. ConcurrentHashMap and CopyOnWriteArrayList should only be used in case you need synchronization.
Good things over here ... keep it up
Very nice article. Very easy to follow..
Awesome articles!! Very precise and clear. Keep it up...
Awesome articles!! Very precise and clear. Keep it up...
Thank nshrestha, Glad to hear that you like this tutorial about synchronized collection and concurrent collection class in java. You may also like 10 Example of hashtable in java
Very recently I have started studying java and found this article to be nice. There are some posts related to database system concepts at crazy4db.blogspot.in for those who may be interested in databases.
A very nice post
for more details we can go in depth of concurrent hashmaps and different types of locks used for more granular synchronization
Post a Comment