WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java Collections · Expert · question 70 of 100

How do you implement a custom Collection that is thread-safe in Java, and what are some use cases for doing so?

📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

To implement a custom thread-safe collection in Java, we can use synchronization mechanisms such as the synchronized keyword or the java.util.concurrent package. The synchronized keyword can be used to synchronize access to shared resources, while the java.util.concurrent package provides thread-safe collections such as ConcurrentHashMap and CopyOnWriteArrayList.

Here is an example of implementing a custom thread-safe collection using the synchronized keyword:

import java.util.ArrayList;

public class CustomList {
    private ArrayList<String> list = new ArrayList<>();

    public synchronized void add(String element) {
        list.add(element);
    }

    public synchronized String get(int index) {
        return list.get(index);
    }

    public synchronized int size() {
        return list.size();
    }
}

In this example, we have created a CustomList class that internally uses an ArrayList to store elements. We have made the methods add(), get(), and size() synchronized to ensure that they can be accessed safely by multiple threads.

Here is an example of implementing a custom thread-safe collection using the java.util.concurrent package:

import java.util.concurrent.CopyOnWriteArrayList;

public class CustomList {
    private CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();

    public void add(String element) {
        list.add(element);
    }

    public String get(int index) {
        return list.get(index);
    }

    public int size() {
        return list.size();
    }
}

In this example, we have used the CopyOnWriteArrayList class from the java.util.concurrent package, which is a thread-safe implementation of the List interface. This class creates a new copy of the underlying array every time a modification is made to the list, which makes it thread-safe but potentially less performant than non-thread-safe implementations.

Some use cases for implementing a custom thread-safe collection in Java include scenarios where multiple threads need to access and modify a shared collection concurrently, such as in multi-threaded server applications or concurrent data processing applications. It is important to ensure thread safety in these scenarios to avoid race conditions and other concurrency issues.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Collections interview — then scores it.
📞 Practice Java Collections — free 15 min
📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

All 100 Java Collections questions · All topics