What are concurrent Collection Classes

What are concurrent Collection Classes

Package java.util.concurrent:

Java utility classes generally useful when use in context with concurrent programming and this package includes few small standardized extensible frameworks as well as some classes that provides useful functionality and are otherwise very difficult to implement.

Concurrent Collections:

This package supplies Collection implementations designed mainly for use in multithreaded contexts and these classes are:

ConcurrentHashMap: Whenever many threads is expected to access given collection ConcurrentHashMap is normally preferable to a synchronized HashMap
ConcurrentSkipListMap: It is normally preferable to a synchronized TreeMap
ConcurrentSkipListSet: It’s preferable when you need sorted container that would be accessed by using multiple threads. These are essentially equivalents of TreeMap and TreeSet for concurrent code.
CopyOnWriteArrayList: It’s preferable over to synchronized ArrayList where expected number of reads and traversals is greatly outnumbering the number of updates to the list.
CopyOnWriteArraySet: It is preferable and well suited for applications where set sizes generally stay small and read-only operations vastly outnumber mutative operations(add, set, remove, etc.), and you need to prevent interference among threads during traversal operation.

“Concurrent” prefix has been used to denote with some classes in this package is shorthand indicating many differences from similar “synchronized” classes. For example java.util.Hashtable and Collections.synchronizedMap(new HashMap()) are synchronized. But ConcurrentHashMap is “concurrent”. Concurrent collection is always thread-safe and it’s not governed by single exclusion lock. In case of ConcurrentHashMap, it will safely permits any number of concurrent thread to reads as well as tunable number of other concurrent writes. “Synchronized” classes could be useful when we need to prevent all access to collection via using single lock but at the expense of poorer scalability.

Leave a Reply

Your email address will not be published. Required fields are marked *