神刀安全网

What is difference between Synchronized and Concurrent Collections in Java?

Synchronized vs Concurrent Collections

Though both Synchronized and Concurrent Collection classes provide thread-safety, the differences between them comes  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 e.g. ConcurrentHashMap , CopyOnWriteArrayList , and CopyOnWriteHashSet . Main reason for this slowness is locking; synchronized collections locks the whole collection e.g. whole Map or List while concurrent collection never locks the whole Map or List. They achieve thread safety by using advanced and sophisticated techniques like lock stripping. For example, the ConcurrentHashMap divides the whole map into several segments and locks only the relevant segments, which allows multiple threads to access other segments of same ConcurrentHashMap without locking.

Similarly, CopyOnWriteArrayList allows multiple reader threads to read without synchronization and when a write happens it copies the whole ArrayList and swap with a newer one.

So if you use concurrent collection classes in their favorable conditions e.g. for more reading and fewer updates, they are much more scalable than synchronized collections.

By the way, Collection classes are the 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 are highly efficient for high volume, low latency Java application.

Synchronized Collections vs Concurrent Collections in Java

The synchronized collections classes,Hashtable andVector, 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, most importantly 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 and scalability while preserving the thread safety with some minor compromises in their promises to callers.

What is difference between Synchronized and Concurrent Collections in Java?


The 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.

You can also check out Core Java Volume 1 – Fundamentals by Cay S. Horstmann to learn more about how concurrent collection works. I have found the explanation given about various things about Java API and programming language in this book very informative and easy to digest. That’s the main reason I highly recommend this book.

What is difference between Synchronized and Concurrent Collections in Java?

That’s all about the difference between synchronized and concurrent collection classes in Java . In Summary concurrent collections use advanced technique to achieve thread-safety without compromising Scalability. For example,ConcurrentHashMaponly locks certain portion of Map while Hashtable locks full map while doing iteration or performing any write operation.

As always comments and suggestions are welcome.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » What is difference between Synchronized and Concurrent Collections in Java?

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址