Skip to content

Commit 6975f84

Browse files
committed
feat: add ConcurrentSkipListMapExample with concurrent writers
What - Implemented `ConcurrentSkipListMapExample` demo. - Demonstrates concurrent writes + iteration using `ConcurrentSkipListMap`. - Showcases sorted operations (`firstKey`, `lastKey`, `subMap`). Why - To illustrate how `ConcurrentSkipListMap` behaves under concurrent updates. - Highlights weakly-consistent iterators that tolerate concurrent modifications. - Shows real-time insertion by two threads (odd/even writers). How - Created `ConcurrentSkipListMap<Integer, String>`. - Preloaded keys `50`, `10`, `30`, `70`. - Spawned two threads: - `writer-odd` → inserts odd keys with values `"vX"`. - `writer-even` → inserts even keys with values `"vX"`. - Iterated concurrently over `map.entrySet()` (may reflect partial updates). - After joining threads, printed: - First/last key - A submap view `[10..30]`. Logic - Iteration is weakly-consistent → doesn’t throw `ConcurrentModificationException`. - Map maintains natural ordering of keys. - Range queries like `subMap`, `headMap`, `tailMap` work efficiently. Real-world use - Thread-safe sorted maps for caches, leaderboards, or time-series data. - Useful when concurrent writes + ordered reads are required. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 5ef321f commit 6975f84

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.util.Map;
2+
import java.util.NavigableMap;
3+
import java.util.concurrent.ConcurrentSkipListMap;
4+
5+
public class ConcurrentSkipListMapExample {
6+
public static void main(String[] args) throws InterruptedException {
7+
ConcurrentSkipListMap<Integer, String> map = new ConcurrentSkipListMap<>();
8+
9+
// Put some entries (not blocking)
10+
map.put(50, "fifty");
11+
map.put(10, "ten");
12+
map.put(30, "thirty");
13+
map.put(70, "seventy");
14+
15+
// Start writers concurrently
16+
Thread writer1 = new Thread(() -> {
17+
for (int i = 1; i <= 100; i += 2) {
18+
map.put(i, "v" + i);
19+
}
20+
}, "writer-odd");
21+
22+
Thread writer2 = new Thread(() -> {
23+
for (int i = 2; i <= 100; i += 2) {
24+
map.put(i, "v" + i);
25+
}
26+
}, "writer-even");
27+
28+
writer1.start();
29+
writer2.start();
30+
31+
// Meanwhile, iterate using weakly consistent iterator
32+
for (Map.Entry<Integer, String> e : map.entrySet()) {
33+
System.out.println(e.getKey() + " -> " + e.getValue());
34+
// Note: iteration may miss or include concurrent updates
35+
}
36+
37+
writer1.join();
38+
writer2.join();
39+
40+
// After writers finish, map is sorted
41+
System.out.println("First key: " + map.firstKey());
42+
System.out.println("Last key: " + map.lastKey());
43+
44+
// Get a subMap view (sorted)
45+
NavigableMap<Integer, String> sub = map.subMap(10, true, 30, true);
46+
System.out.println("Submap [10..30]: " + sub);
47+
}
48+
}

0 commit comments

Comments
 (0)