Sabtu, 08 Februari 2014

What is difference between Synchronized and Concurrent Collections in Java?



Synchronized vs Concurrent Collections

Though both Synchronized and Concurrent Collection class provide thread-safety, main difference between them is in performance, scalability and how they achieve thread-safety. Synchronized collections like synchronized HashMap, Hashtable, HashSet, Vector and synchronized ArrayList are much slower than their concurrent counterparts ConcurrentHashMap, CopyOnWriteArrayList and CopyOnWriteHashSet. Main reason of this slowness is locking, synchronized collection locks whole object, while concurrent collection achieves thread-safety smartly e.g. ConcurrentHashMap divides the whole map into several segments and locks only on segments, which allows multiple threads to access other collections without locking. CopyOnWriteArrayList allows multiple reader thread to read without synchronization and when their is write happens it copies the ArrayList and swaps. So if you use concurrent collection classes in their favorable conditions e.g. for more reading and less updates, they are much more scalable than synchronized collections. By the way, Collection classes are heart of Java API though I feel using them judiciously is an art. It's my personal experience where I have improved performance by using ArrayList where legacy codes are unnecessarily used Vector etc. JDK 1.5 introduce some good concurrent collections which is highly efficient for high volume, low latency Java application.




Synchronized Collections vs Concurrent Collections in Java


The synchronized collections classes, Hashtable and Vector, and the synchronized wrapper classes, Collections.synchronizedMap() and Collections.synchronizedList(), provides 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 ConcurrentModificationException.



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 multi-threaded 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 introduced concept of segmentation, It doesn't mater whether how large it becomes because 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 locks certain portion of Map while Hashtable lock full map while doing iteration or performing any write operation.



As always comments and suggestions are welcome.























Source:http://javarevisited.blogspot.com/2010/10/what-is-difference-between-synchronized.html

Tidak ada komentar:

Posting Komentar