|
| 1 | +ConcurrentHashMap — Internal Working |
| 2 | + |
| 3 | +1. Java 7 Internal Working (Segment-based Locking): |
| 4 | + - Table divided into **16 Segments** by default. |
| 5 | + - Each segment = smaller HashMap with its own lock. |
| 6 | + - **Read**: doesn’t require locking (unless a write happens in the same segment). |
| 7 | + - **Write**: locks only that segment, not the whole map. |
| 8 | + - Pros: Better concurrency than Hashtable (coarse lock). |
| 9 | + - Cons: Still limited concurrency (max ~16 threads writing simultaneously). |
| 10 | + |
| 11 | +2. Java 8 Redesign (No Segments): |
| 12 | + - Segments removed → single table of buckets. |
| 13 | + - Uses **CAS (Compare-And-Swap)** + **per-bucket synchronized locking**. |
| 14 | + - **Reads**: completely lock-free, rely on volatile reads. |
| 15 | + - **Writes**: |
| 16 | + • Try CAS to insert new node in empty bucket. |
| 17 | + • If CAS fails → synchronize on bucket’s first node and update. |
| 18 | + - **Treeification**: If bucket too long (≥8), convert linked list → balanced Red-Black Tree (log n lookup). |
| 19 | + - **Resizing**: Cooperative transfer. Multiple threads help move entries using `ForwardingNode`. |
| 20 | + - **Counters**: Uses `LongAdder`-like striped counters to track size (reduces contention). |
| 21 | + - Result: Highly scalable with near lock-free reads and minimal write contention. |
| 22 | + |
| 23 | +3. CAS Example: |
| 24 | + - Thread A last saw `x = 45`. |
| 25 | + - It wants to update → `x = 50`. |
| 26 | + - CAS: "If x is still 45, set to 50; else retry." |
| 27 | + - This ensures atomic update without full locks. |
| 28 | + |
| 29 | +4. ConcurrentSkipListMap (Contrast): |
| 30 | + - MAP → SORTED → THREAD-SAFE. |
| 31 | + - Unlike ConcurrentHashMap (unsorted), it maintains keys in **sorted order** using a **Skip List**. |
| 32 | + - Operations (`get`, `put`, `remove`) take O(log n). |
| 33 | + - Also thread-safe, supports concurrent access like CHM. |
| 34 | + - Useful when you need both **ordering** + **concurrency**. |
| 35 | + |
| 36 | +5. ASCII Diagram (CHM bucket flow): |
| 37 | + |
| 38 | + Table (array of buckets) |
| 39 | + +--------------------------------------------------+ |
| 40 | + | 0 | 1 | 2 | 3 | ... | |
| 41 | + +--------------------------------------------------+ |
| 42 | + ↓ |
| 43 | + CAS insert Node(key, val) |
| 44 | + ↓ |
| 45 | + If collision → LinkedList / TreeBin |
| 46 | + ↓ |
| 47 | + If chain length > 8 → convert to Tree (log n) |
| 48 | + ↓ |
| 49 | + If threshold exceeded → resize (ForwardingNode helps migration) |
| 50 | + |
| 51 | +6. Key Takeaways: |
| 52 | + ✔ Java 7 → segment-locking (limited concurrency). |
| 53 | + ✔ Java 8 → CAS + per-bucket sync (better scaling). |
| 54 | + ✔ Reads are always lock-free. |
| 55 | + ✔ Treeification avoids long chains. |
| 56 | + ✔ ConcurrentSkipListMap = sorted + thread-safe alternative. |
| 57 | + |
| 58 | +Applications: |
| 59 | + - CHM → Caches, registries, concurrent counters, shared state. |
| 60 | + - CSLM → Leaderboards, ordered concurrent maps, financial apps. |
0 commit comments