Member-only story
Java Multithreading, Concurrency, and Parallelism — Part 17.1
Introduction to Concurrent Collections — CopyOnWriteArrayList

In the previous part-16, we have seen the ReentrantLock
and Condition
objects to implement Producer
-Consumer
patterns. Now we will look at the concurrent collections offered by the java concurrency framework. Most of these collections internally use ReentrantLocks along with Volatiles and Compare-And-Swap (CAS). CAS offers a technique to implement non-blocking mechanisms. We will look at CAS in-depth in later parts.
As we all know concurrent collections provide thread safety as they yield consistent results if multiple threads add or delete elements to and from the collection. If you had read and understood all the previous parts of this series, you might have understood why we need to go for concurrent collections. Why not synchronized
collections? Ah! we didn’t mention synchronized
collections so far, right?. Here are they. The synchronized
collections are very simple. All the methods in these collections like add
, remove
, get
, insert
and etc are synchronized
. We have the following static utility methods in java.util.Collections
class to create synchronized
collections. The below code snippet is extracted from java.util.Collections.synchronizedList()
method that returns the SynchronizedList
object.