CopyOnWrite Collections

Welcome to CopyOnWrite Collections

Building on your foundational knowledge of synchronized and concurrent collections, you're now ready to explore CopyOnWrite collections in Java. This type of collection is particularly effective for scenarios involving frequent reads and infrequent writes, offering thread safety without the performance drawbacks of locking.

What You'll Learn

By the end of this lesson, you will understand:

  • What CopyOnWrite collections are and how they function.
  • Practical applications of CopyOnWriteArrayList and its benefits.
  • How to implement a thread-safe subscriber list using CopyOnWriteArrayList.

By mastering these concepts, you'll be equipped to handle situations that demand high efficiency in reading data while maintaining thread safety.

Understanding CopyOnWrite Collections

Copy-on-Write (CoW) collections, like CopyOnWriteArrayList, are designed to optimize performance in situations where read operations are much more frequent than write operations.

The core principle behind these collections is that every time a modification (such as adding or removing an element) occurs, a new copy of the entire internal array is created. This allows read operations to happen concurrently without synchronization or locking. Since reads don't alter the data, they safely access the original array while modifications occur on a new copy.

CopyOnWriteArrayList Design

The design of the CopyOnWriteArrayList ensures thread safety without requiring synchronization. Each time a method like add() or remove() is called, the current content of the list is copied into a new array. This ensures read consistency because readers use the old array, while modifications are done on the new one.

This approach is ideal for situations that are read-heavy with only occasional writes or updates.

Let's break down how to use CopyOnWriteArrayList with some common scenarios.

Creating and Adding Elements

Creating and adding elements to a CopyOnWriteArrayList is simple. Each time an element is added, a new internal array is created to include the added elements.

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class SubscriberList {
    private List<String> subscribers = new CopyOnWriteArrayList<>();
    
    public void addSubscriber(String subscriber) {
        subscribers.add(subscriber);
    }

    public static void main(String[] args) {
        SubscriberList list = new SubscriberList();
        list.addSubscriber("subscriber1");
        list.addSubscriber("subscriber2");
        
        System.out.println(list.subscribers);
    }
}

In this snippet, we initialize the subscribers list using CopyOnWriteArrayList. This ensures thread safety, meaning multiple threads can read from the list concurrently without locking. When we call subscribers.add(subscriber);, a new array is created behind the scenes to include the new subscriber. This way, the old array remains unchanged for any ongoing read operations, which makes it optimal for read-heavy use cases.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal