Java Concurrency Interview

Concurrent collections question 72 of 180 mid

What is ConcurrentSkipListMap for, and how does it differ from ConcurrentHashMap?

When you need ordering, range queries, firstKey, headMap or ceilingEntry from several threads, there is no concurrent TreeMap. ConcurrentSkipListMap fills that gap with a skip list, which is a probabilistic balanced structure that can be updated with CAS rather than the rotations a red-black tree would need under a lock.

The trade-off is the usual one: O(log n) instead of O(1) and worse constants, so do not use it as a general-purpose map. Since Java 9 its size() reads a LongAdder, so it is cheap; on Java 8 it walked the whole list.

collections sorted

Quiz yourself on Concurrent collections Suggest a correction