In Java 8 and later, Collections.synchronizedNavigableSet(s) returns a thread-safe NavigableSet wrapper. Single calls such as ceiling, floor, pollFirst, and pollLast each run under one lock.
pollFirst() reads and removes in one call, so it is a ready-made atomic "take the next one". Combinations like ceiling followed by remove still need synchronized (set) { ... }.
A static method of the java.util.Collections utility class, available since Java 8. Call it on the class name, as in Collections.synchronizedNavigableSet(...), after import java.util.Collections;. You never create a Collections object.
Syntax
Wrap a new TreeSet.
Java
NavigableSet<Integer> free = Collections.synchronizedNavigableSet(new TreeSet<Integer>());
Integer seat = free.pollFirst(); // atomic take
synchronized (free) { // compound: find then remove
Integer s = free.ceiling(4);
if (s != null) free.remove(s);
}
Call
What it does
pollFirst() / pollLast()
Atomic read-and-remove.
ceiling / floor / higher / lower
Atomic single lookups.
descendingSet() / headSet(x, incl)
Views guarded by the same lock.
synchronized (set) { ... }
Needed for loops and multi-step logic.
Added in Java 8. On Java 6 or 7 you can wrap a TreeSet with synchronizedSortedSet, but you lose the navigation methods on the wrapper type.
Library declarations
Java
// java.util.Collections (Java 8+)
public static <T> NavigableSet<T> synchronizedNavigableSet(NavigableSet<T> s)
Parameters
Parameter
Description
s
The navigable set to make thread-safe. Do not use it directly afterwards.
Exact hit. Seat 4 is free, so ceiling(4) returns it and it is removed.
Next best. The second request for 4 finds it gone; ceiling moves up to 5.
Nothing high enough. No seat is 10 or above, so the result is null. Holding the lock ensures no other thread takes the seat between lookup and removal.
Example 3 — Expire Old Timestamps, Read Newest First
Clear a head view to drop old entries, then iterate the descending view under the lock.
Java
import java.util.Arrays;
import java.util.Collections;
import java.util.NavigableSet;
import java.util.TreeSet;
public class CollectionsSynchronizedNavigableSetExpire {
public static void main(String[] args) {
NavigableSet<Integer> times = Collections.synchronizedNavigableSet(
new TreeSet<>(Arrays.asList(100, 140, 205, 260, 300)));
times.headSet(200, false).clear();
StringBuilder sb = new StringBuilder();
synchronized (times) {
for (int t : times.descendingSet()) {
sb.append(t).append(' ');
}
}
System.out.println("newest first: " + sb.toString().trim());
System.out.println("lower(260) = " + times.lower(260));
}
}
Output
newest first: 300 260 205
lower(260) = 205
How It Works
Bulk expiry.headSet(200, false) is a view of times below 200; clearing it deletes 100 and 140 from the real set.
Descending loop. The reverse view still needs synchronized (times) while iterating, locking the original wrapper.
Navigation.lower(260) returns the closest time strictly below 260, which is 205.
Frequently Asked Questions
It returns a thread-safe NavigableSet wrapper; each call, including ceiling and pollFirst, holds one lock.
Yes. It reads and removes the lowest element in a single locked call.
No. Put both calls inside synchronized (set) { ... }.
Java 8.
Yes. Synchronize on the original wrapper for the whole loop.