Fundamentals 8 min read

Understanding ConcurrentSkipListSet in Java: Overview, Principles, API, and Example

This article explains the thread‑safe, ordered Java collection ConcurrentSkipListSet, compares it with TreeSet, describes its skip‑list based internal structure, lists its full API, and provides a multithreaded example demonstrating correct concurrent usage versus the failure of TreeSet.

Big Data Technology & Architecture
Big Data Technology & Architecture
Big Data Technology & Architecture
Understanding ConcurrentSkipListSet in Java: Overview, Principles, API, and Example

ConcurrentSkipListSet is a thread‑safe, ordered set in Java, suitable for high‑concurrency scenarios.

It differs from TreeSet in that it provides built‑in thread safety and is implemented on top of ConcurrentSkipListMap, inheriting the NavigableSet interface.

The internal structure is a skip‑list; each node contains a key and forward pointers, enabling logarithmic‑time search, insertion, and removal even under concurrent access.

The class offers several constructors and a rich set of methods, including basic operations (add, remove, contains, clear, size, isEmpty), navigation methods (first, last, ceiling, floor, higher, lower, pollFirst, pollLast), and view methods (headSet, tailSet, subSet, descendingSet, descendingIterator).

Below is the full method list extracted from the JDK documentation:

// Constructors
ConcurrentSkipListSet()
ConcurrentSkipListSet(Collection<? extends E> c)
ConcurrentSkipListSet(Comparator<? super E> comparator)
ConcurrentSkipListSet(SortedSet<E> s)

// Basic operations
boolean add(E e)
E ceiling(E e)
void clear()
ConcurrentSkipListSet<E> clone()
Comparator<? super E> comparator()
boolean contains(Object o)
Iterator<E> descendingIterator()
NavigableSet<E> descendingSet()
boolean equals(Object o)
E first()
E floor(E e)
NavigableSet<E> headSet(E toElement)
NavigableSet<E> headSet(E toElement, boolean inclusive)
E higher(E e)
boolean isEmpty()
Iterator<E> iterator()
E last()
E lower(E e)
boolean remove(Object o)
boolean removeAll(Collection<?> c)
int size()
NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
NavigableSet<E> subSet(E fromElement, E toElement)
NavigableSet<E> tailSet(E fromElement)
NavigableSet<E> tailSet(E fromElement, boolean inclusive)

A concrete example demonstrates creating a ConcurrentSkipListSet, launching two threads that each add ten uniquely named elements, and printing the set after each insertion. The program runs without errors, illustrating the collection’s ability to handle concurrent modifications.

import java.util.*;
import java.util.concurrent.*;

public class ConcurrentSkipListSetDemo1 {
    private static Set<String> set = new ConcurrentSkipListSet<>();

    public static void main(String[] args) {
        new MyThread("a").start();
        new MyThread("b").start();
    }

    private static void printAll() {
        Iterator<String> iter = set.iterator();
        while (iter.hasNext()) {
            System.out.print(iter.next() + ", ");
        }
        System.out.println();
    }

    private static class MyThread extends Thread {
        MyThread(String name) { super(name); }
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                String val = Thread.currentThread().getName() + (i % 6);
                set.add(val);
                printAll();
            }
        }
    }
}

Sample output shows interleaved elements from both threads, confirming that ConcurrentSkipListSet maintains consistency under concurrent access, whereas replacing the set with a TreeSet would trigger a ConcurrentModificationException.

a1, b1, 
 a1, a1, a2, b1, 
 b1, a1, a2, a3, b1,
 ... (truncated for brevity) ...
 a0, a1, a2, a3, a4, a5, b0, b1, b2, b3, b4, b5,
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javathread safetyData StructuresexampleConcurrent CollectionsConcurrentSkipListSet
Big Data Technology & Architecture
Written by

Big Data Technology & Architecture

Wang Zhiwu, a big data expert, dedicated to sharing big data technology.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.