How to Synchronize ArrayList in Java

This post shows how to synchronize ArrayList in Java and what other thread safe alternatives are available to use.

ArrayList in Java is not thread safe as it is not synchronized by default. If you are using ArrayList in a multi-threaded environment where it is accessed by multiple threads concurrently and it is structurally modified too by even a single thread then it must be synchronized externally. A structural modification is defined as any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.

Options for thread-safe List

If you want to synchronize ArrayList in Java or looking for a thread-safe alternative of ArrayList then there are following options.

  1. Using Vector class– Vector is synchronized and a thread safe implementation of List. But the problem is all of its methods are synchronized on a single lock so at any time only oe thread can use Vector even if it is get() method. That makes Vector very slow to use.
  2. Using Collections.synchronizedList() method– Using this method you can synchronize ArrayList.
  3. Using CopyOnWriteArrayList– Another option is to use CopyOnWriteArrayList which is a thread-safe variant of ArrayList. In CopyOnWriteArrayList all mutative operations (add, set) are implemented by making a fresh copy of the underlying array. Since a new copy is made every time List is changed that is why using CopyOnWriteArrayList is ordinarily too costly. It may be more efficient if you have more traversal operations than mutations and you don’t want to synchronize traversals.

Using Collections.synchronizedList() method

Before seeing an example of synchronizing ArrayList in Java using Collections.synchronizedList() method let’s see what may happen if you use ArrayList in a multi-threaded environment with out synchronizing it.

Java program

In the Java program four threads are created, each of these thread adds 5 elements to the list. After all the threads are done list size should be 20.

Output

As you can see in one of the run size of the list is 15 because of the thread interference.

Here is the same example again where ArrayList is synchronized to make it thread safe.

Output

Iterating a synchronized List

As per Java docs even if you use Collections.synchronizedList() to get a synchronized list, it is imperative that you manually synchronize on the returned list when traversing it via Iterator, Spliterator or Stream:

Using CopyOnWriteArrayList

Another option to have a thread safe list is to use CopyOnWriteArrayList. Since a new copy of the list is made if there is any mutation so there is no thread interference.
Let’s see it using a simple example where a CopyOnWriteArrayList is created and then iterated. While iteration an element is removed using the List’s remove method, it still won’t throw ConcurrentModificationException. In the output you can see iteration is still displaying all the elements as iteration is done on a separate copy of CopyOnWriteArrayList.

Output

That’s all for the topic How to Synchronize ArrayList in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.