How CopyOnWriteArrayList works internally in Java?

How CopyOnWriteArrayList works internally in Java maybe not one of the most popular question to ask in interviews but it becomes relevant when trying to find the concurrent data-structure based on list. Suddenly, comparison between ArrayList and CopyOnArrayList becomes natural..

CopyOnArrayList is to ArrayList what ConcurrentHashMap is to HashMap. It is the concrete  concurrent implementation of the List Abstract datatype.

Lets dive straight into the internal workings of CopyOnWriteArrayList.

How add() method of CopyOnWriteArrayList works internally in Java

If you know How ArrayList works internally in Java, it will be very easy for you to understand how CopyOnWriteArrayList works internally in Java.

So, to proceed further, please go through How ArrayList works internally in Java. If you have already gone through that, then it’s great!

At this point you know the internal workings of ArrayList.

Internal working of CopyOnWriteArrayList differs from internal working of ArrayList on two points:

  1. Difference in the array size increment value.
  2. Use of locking for concurrent programming scenario.

Lets discuss each of the above points one by one:

Difference in the array size increment value

In CopyOnWriteArrayList, there is no any concept of default capacity and capacity as such.So, in that regard there is no any logic to calculate the same. Hence, all the functions like ensureCapacity, ensureCapacityInternal, ensureCapacityExternal and grow are missing which are present in ArrayList.

For every addition of the element in CopyOnWrite ArrayList, below things happens in regards to underlying array size:

  • Size of array gets increment by 1.
  • New Array gets created with incremented size.
  • All elements gets copied.
  • New element is added.
  • Underlying array is replaced with new Array with increased size (+1).

All these operation occurs with the help of Arrays.copyOf function which further calls System.arrayCopy().

Use of locking for concurrent programming scenario

The above point was bonus (but crucial), when we are trying to deduce the internals and comparison of CopyOnWriteArrayList. The main difference lies on how it handles concurrency.

The reason why we choose CopyOnWriteArrayList over ArrayList is the case of concurrent environment.

For every mutator operation, the operations where change comes in the underlying array, it acquires lock and releases it after the operation.

The lock used is Reentrant lock.

Let me show you some code:

public boolean add(E paramE) {
    ReentrantLock localReentrantLock = this.lock;
    try {
        //add operation
    finally {

This locking mechanism is for all the mutator operations like all variance of add(), remove() and set().

Thanks for reading. Let me know your thoughts and questions on this article in the comment section below.

Ankush Pahwa is an IT Majdoor (Labourer) in Indian IT industry for last 5 years. Don’t know what else to say, so stopping here.

Self-Learning -> Focused Group -> Learning Intensification -> Self-Learning

Have any Question or Comment?

Leave a Reply

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

LinkedIn Auto Publish Powered By :