Skip to content

Commit cdca1ac

Browse files
committed
ConcurrentSkipListMap — Internal working.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent ce1bcb9 commit cdca1ac

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
Internal working — java.util.concurrent.ConcurrentSkipListMap
2+
3+
Overview
4+
--------
5+
ConcurrentSkipListMap is a concurrent, sorted implementation of the `NavigableMap`
6+
interface. It is built on a skip list rather than a tree or hash table, which makes
7+
it easier to support concurrent operations with minimal locking.
8+
9+
Key properties:
10+
- Sorted according to natural order of keys, or a custom Comparator.
11+
- Thread-safe: supports concurrent reads and writes with high throughput.
12+
- Non-blocking (mostly lock-free) reads; updates use CAS (Compare-And-Swap).
13+
- Iterators are *weakly consistent*: tolerate concurrent modifications, do not
14+
throw ConcurrentModificationException, and reflect some but not necessarily
15+
all changes made during iteration.
16+
- Null keys and null values are not allowed.
17+
18+
Core ideas
19+
----------
20+
- Backing structure: skip list (multi-level linked list).
21+
- Search: start at top level, move right until next would overshoot, then drop down.
22+
- Insert: splice new node into multiple levels with CAS.
23+
- Remove: logically mark node removed, then unlink with CAS.
24+
- Expected average cost: O(log n) for get, put, remove.
25+
26+
ASCII structure (simplified skip list)
27+
--------------------------------------
28+
29+
Level 3: HEAD ----------------------> [50] -----------------------> null
30+
Level 2: HEAD -------> [20] -> [50] -> [90] -> null
31+
Level 1: HEAD -> [10] -> [20] -> [30] -> [50] -> null
32+
Level 0: HEAD -> [5] -> [10] -> [15] -> [20] -> [25] -> [30] -> [40] -> [50] -> null
33+
34+
- Each higher level “skips” more elements → faster search.
35+
- Nodes appear at random levels (probabilistic balancing).
36+
37+
Operation flows
38+
---------------
39+
40+
PUT (insert/update):
41+
1. Locate predecessors at each level.
42+
2. Build new node with forward pointers.
43+
3. CAS to insert at level 0; if success, try higher levels.
44+
4. Retry if CAS fails due to contention.
45+
46+
GET (search):
47+
1. Traverse levels down to level 0.
48+
2. Move right until candidate key ≥ target.
49+
3. If equal → return value; else return null.
50+
51+
REMOVE:
52+
1. Find node, mark logically removed.
53+
2. CAS predecessors to unlink it.
54+
3. GC reclaims unreachable nodes.
55+
56+
Thread-safety model
57+
-------------------
58+
- High concurrency: readers never block.
59+
- Writers use fine-grained CAS to update only local pointers.
60+
- No global lock (unlike Hashtable); avoids contention bottlenecks.
61+
62+
Use cases
63+
---------
64+
- Concurrent sorted maps (priority queues, leaderboards, indexes).
65+
- Time-based scheduling (keys as timestamps).
66+
- When you need fast concurrent access + ordering guarantees.
67+
68+
Complexity summary
69+
------------------
70+
- get/put/remove: O(log n) expected.
71+
- Iteration: O(n).
72+
- Memory: more than TreeMap (extra forward pointers), but predictable.

0 commit comments

Comments
 (0)