Programming for fun and profit

Programming tutorials, problems, solutions. Always with code.

Java synchronized List


How to use Java List in a thread-safe way? It can be synchronized manually or you can use a wrapper method from Java Collections Framework. Learn how to use it correctly.


In the following example we use synchronizedList(List) from java.util.Collections class. The original list should be accessed only using thread-safe wrapper, returned from the method. The synchronized version is called protection proxy (see Proxy design pattern in Gang of Four), because it delegates calls to the original list, but only verifies that correct conditions have occurred, here – correctly synchronized access.

Here’s the Java code:


import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class CollectionsSynchronizedList {

    public static void main(String[] args) {
        List<String> list = Collections.synchronizedList(
                new LinkedList<>(Arrays.asList("a", "b", "c")));
        System.out.println("thread-safe list: " + list);

        // single operations are atomic:

        // multiple operations have to be synchronized:
        synchronized (list) {
            for (Iterator<String> it = list.iterator(); it.hasNext(); ) {
                System.out.println("item: " +;

Single operations on synchronized list are guaranteed to be atomic, but if you want to do many operations in consistent manner, then you have to synchronize that.

The result is simple:

thread-safe list: [a, b, c]
item: a
item: b
item: c
item: d

java.util.Collections has methods that creates synchronized wrappers for different types of Java Collections:

  • <T> Collection<T> synchronizedCollection(Collection<T> c):
    Returns a synchronized (thread-safe) collection backed by the specified collection.
  • <T> List<T> synchronizedList(List<T> list):
    Returns a synchronized (thread-safe) list backed by the specified list.
  • <K,V> Map<K,V> synchronizedMap(Map<K,V> m):
    Returns a synchronized (thread-safe) map backed by the specified map.
  • <K,V> NavigableMap<K,V> synchronizedNavigableMap(NavigableMap<K,V> m) (since Java 1.8):
    Returns a synchronized (thread-safe) navigable map backed by the specified navigable map.
  • <T> NavigableSet<T> synchronizedNavigableSet(NavigableSet<T> s) (since Java 1.8):
    Returns a synchronized (thread-safe) navigable set backed by the specified navigable set.
  • <T> Set<T> synchronizedSet(Set<T> s):
    Returns a synchronized (thread-safe) set backed by the specified set.
  • <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m):
    Returns a synchronized (thread-safe) sorted map backed by the specified sorted map.
  • <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s):
    Returns a synchronized (thread-safe) sorted set backed by the specified sorted set.

As you can see java.util.Collections is very rich. :-)

Share with the World!