Concurrent Collections in java

Concurrent Collections in java:

The java.util.concurrent package includes some classes and interfaces related to the Java Collections Framework.

Below are the concurrent collection interfaces –

BlockingQueue defines a first-in-first-out data structure that blocks or times out when you attempt to add to a full queue, or retrieve from an empty queue.

ConcurrentMap is a subinterface of java.util.Map that defines useful atomic operations.

ConcurrentNavigableMap is a subinterface of ConcurrentMap that supports approximate matches.

BlockingDeque – Its a dequeue that supports blocking operations that wait for the deque to become non-empty when retrieving an element, and wait for space to become available in the deque when storing an element.

TransferQueue – Its a BlockingQueue in which producers may wait for consumers to receive elements.

Some useful concurrent collection classes –

All classes below are thread safe.

ConcurrentHashMap:

This class provides the same functionality as Hashtable, and includes same versions of methods corresponding to each method of Hashtable. All the operations(like get, put, remove) on this class are thread-safe, retrieval operations do not lock the whole map, and there is not any support for locking the entire table that prevents all access. Iterator of ConcurrentHashMap are fail-safe iterators which doesn’t throw ConcurrencModificationException.

CopyOnWriteArrayList:

Its a thread-safe version of ArrayList in which all mutative operations (like add, set etc.) are implemented by making a fresh copy of the underlying array. you can use this class in multithreading but it cost more as it creates a new copy of array for mutative operations. Like ArrayList it also accepts null.

CopyOnWriteArraySet:

Its a Set that uses an internal CopyOnWriteArrayList for all of its operations. It is good to use when set size is small and operations like (add, set, remove, etc.) are expensive since they usually entail copying the entire underlying array.

BlockingQueue :

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.

ConcurrentSkipListMap:

This class implements a concurrent version of SkipLists and it implements ConcurrentNavigableMap. This class implements a tree-like two-dimensionally linked skiplist in which the index levels are represented in separate nodes from the base nodes holding data.

Gopal Das
Follow me

Gopal Das

Founder at GopalDas.Org
He is a technology evangelist, Salesforce trainer, blogger, and working as a Salesforce Technical Lead. After working in Java based project implementation, he jumped to the Salesforce system on a whim and never looked back. He fell in love with Salesforce’s flexibility, scalability, and power. He expanded his knowledge of the platform and became a Certified App Builder, Administrator, Platform Developer I, SalesCloud Consultant while leading the Salesforce implementation and technology needs. He has worked in a wide variety of applications/services like desktop, web and mobile applications.
Gopal Das
Follow me

Leave a Reply

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