Concurrent Collections in Java

Sourin Sutradhar
Programming Notes
Published in
2 min readNov 8, 2016

The Java collection classes provides a wide range of classes and interfaces that makes our life simple and our code elegant.

However, most of these classes are not thread-safe and one must be careful while implementing them in a multi-threaded environment. Several new Collection classes are added in Java 5 and Java 6 specially concurrent alternatives of standard synchronized ArrayList, HashTable and synchronized HashMap collection classes.

Iterator fail-safe property work with the clone of underlying collection, hence it’s not affected by any modification in the collection. By design, all the collection classes in java.util package are fail-fast whereas collection classes in java.util.concurrent are fail-safe.
Fail-fast iterators throw ConcurrentModificationException whereas fail-safe iterator never throws ConcurrentModificationException

Here we discuss the most commonly used concurrent collections that are part of the java.util.concurrent package.

ConcurrentHashMap

ConcurrentHashMap provides a concurrent alternative of HashTable or Synchronized Map classes with aim to support higher level of concurrency by implementing fined grained locking. Multiple reader can access the Map concurrently while a portion of Map gets locked for write operation depends upon concurrency level of Map. ConcurrentHashMap provides better scalability than there synchronized counter part. Iterator of ConcurrentHashMap are fail-safe iterators which doesn’t throw ConcurrentModificationException thus eliminates another requirement of locking during iteration which result in further scalability and performance.

Here is a brilliant writeup explaining the difference between a HashMap and ConcurrentHashMap.

CopyOnWriteArrayList and CopyOnWriteArraySet

CopyOnWriteArrayList is a concurrent alternative of synchronized List. CopyOnWriteArrayList provides better concurrency than synchronized List by allowing multiple concurrent reader and replacing the whole list on write operation. Yes, write operation is costly on CopyOnWriteArrayList but it performs better when there are multiple reader and requirement of iteration is more than writing. Since CopyOnWriteArrayList Iterator also don’t throw ConcurrentModificationException it eliminates need to lock the collection during iteration. Remember both ConcurrentHashMap and CopyOnWriteArrayList doesn’t provides same level of locking as Synchronized Collection and achieves thread-safety by there locking and mutability strategy. So they perform better if requirements suits there nature. Similarly, CopyOnWriteArraySet is a concurrent replacement to Synchronized Set.

Here is a brilliant writeup explaining the difference between a ArrayList and CopyOnWriteArrayList.

BlockingQueue

BlockingQueue is also one of better known collection class in Java 5. BlockingQueue makes it easy to implement producer-consumer design pattern by providing inbuilt blocking support for put() and take() method. put() method will block if Queue is full while take() method will block if Queue is empty. Java 5 API provides two concrete implementation of BlockingQueue in form of ArrayBlockingQueue and LinkedBlockingQueue, both of them implement FIFO ordering of element. ArrayBlockingQueue is backed by Array and its bounded in nature while LinkedBlockingQueue is optionally bounded. Consider using BlockingQueue to solve producer Consumer problem in Java instead of writing your won wait-notify code. Java 5 also provides PriorityBlockingQueue, another implementation of BlockingQueue which is ordered on priority and useful if you want to process elements on order other than FIFO.

This concept is well explained in these set of tutorials by Jenkov : BlockingQueue , ArrayBlockingQueue , LinkedBlockingQueue and PriorityBlockingQueue.

Thanks. I will keep on adding more concurrent collections here.

--

--