ConcurrentSkipListSet

About

ConcurrentSkipListSet is a thread-safe, high-performance implementation of a NavigableSet that is backed by a ConcurrentSkipListMap. It maintains elements in their natural order or a custom order specified by a Comparator. This class is particularly suitable for concurrent applications where multiple threads need efficient and consistent access to a sorted set.

Features

  1. Thread-Safe: Supports concurrent access by multiple threads without requiring external synchronization.

  2. Sorted: Maintains elements in natural or custom order.

  3. Non-Blocking: Uses lock-free algorithms internally for high performance in concurrent environments.

  4. Navigable: Provides methods for retrieving elements relative to a given element, such as lower(), floor(), ceiling(), and higher().

  5. Iterators Are Weakly Consistent: Iterators reflect the state of the set at the time they are created but may not include subsequent modifications.

  6. No Nulls Allowed: Does not permit null elements, as null values cannot be reliably compared.

Internal Working

Skip List Structure

  • Internally, ConcurrentSkipListSet is implemented using a skip list.

  • A skip list is a hierarchical structure where elements are organized into multiple levels of linked lists. The lowest level contains all elements in sorted order, and higher levels contain fewer elements, which act as "express lanes" for faster traversal.

Concurrent Modifications

  • Uses a lock-free algorithm to ensure thread-safety.

  • Operations like insertion, deletion, and search are performed atomically without locking the entire structure.

  • This ensures high throughput in multi-threaded environments.

Backing Map

  • The ConcurrentSkipListSet is backed by a ConcurrentSkipListMap, where the keys represent the elements of the set.

  • Each key in the map corresponds to an element in the set, and the value associated with the key is a dummy object (used for internal purposes).

Node Structure

  • Each node in the skip list contains references to other nodes at various levels, enabling efficient jumps during traversal.

Key Methods

Method

Description

add(E e)

Adds the specified element to the set.

remove(Object o)

Removes the specified element from the set.

contains(Object o)

Returns true if the set contains the specified element.

size()

Returns the number of elements in the set.

clear()

Removes all elements from the set.

iterator()

Returns an iterator over the elements in the set in ascending order.

descendingIterator()

Returns an iterator over the elements in descending order.

ceiling(E e)

Returns the least element greater than or equal to the given element.

floor(E e)

Returns the greatest element less than or equal to the given element.

higher(E e)

Returns the least element strictly greater than the given element.

lower(E e)

Returns the greatest element strictly less than the given element.

first()

Returns the first (lowest) element in the set.

last()

Returns the last (highest) element in the set.

subSet(E from, E to)

Returns a view of the portion of the set within the specified range.

Big-O for Operations

Operation

Time Complexity

Add

O(log n)

Remove

O(log n)

Contains

O(log n)

Iteration

O(n)

Limitations

  1. Memory Overhead: Due to its hierarchical structure, it uses more memory than simpler data structures like TreeSet.

  2. Relatively Slower Than Non-Thread-Safe Counterparts: Slightly slower than TreeSet in single-threaded environments because of the overhead of thread-safety mechanisms.

  3. No Null Elements: Does not support null values as they are incompatible with natural or custom ordering.

Real-World Usage

  1. Concurrent Task Scheduling: Storing and retrieving tasks in sorted order for execution in a multi-threaded application.

  2. Thread-Safe Logs: Maintaining a thread-safe collection of sorted timestamps or events.

  3. Sorted Caches: Implementing thread-safe, sorted caches with predictable iteration order.

Examples

1. Basic Operations

2. Navigable Methods

3. Concurrent Access

Last updated