Commit 908934c
committed
feat(ConcurrentHashMapDemo1): add demo of thread-safe updates with ConcurrentHashMap
What
- Added ConcurrentHashMapDemo1 class.
- Created ConcurrentHashMap<String,Integer> with initial capacity 16.
- Spawned two threads:
- Thread t1 inserts 1000 key-value pairs: "A0"→0 through "A999"→999 using put().
- Thread t2 runs 1000 iterations, computing keys "B"+(i % 500):
• Uses computeIfAbsent to insert 500 distinct keys "B0".."B499".
• Each key attempted twice; only the first attempt succeeds.
- Used CountDownLatch to wait for both threads to finish.
- Printed expected size (1500), actual size, and sample lookups for "A10" and "B10".
Why
- Demonstrates concurrent modifications to a map without external synchronization.
- Shows thread-safety of ConcurrentHashMap for both put() and computeIfAbsent().
- Explains that computeIfAbsent is atomic per key, ensuring no race conditions.
- Highlights that iteration order is not preserved in ConcurrentHashMap.
How
- map.put(key,val) directly inserts/updates entries.
- map.computeIfAbsent(key, mappingFunction):
- Executes mapping function only if key is absent.
- Ensures atomic insertion per key.
- CountDownLatch ensures main thread waits until both workers complete.
- map.size() checked after inserts.
Logic
- Inputs:
- Thread t1: 1000 unique "A"-keys.
- Thread t2: 1000 iterations but only 500 unique "B"-keys due to modulo.
- Outputs:
- Expected map size = 1500.
- Actual map size printed to verify correctness.
- Example lookups for A10 and B10.
- Flow:
1. Initialize map and latch.
2. Start t1 (inserts A-keys).
3. Start t2 (inserts B-keys).
4. Await latch.
5. Print stats.
- Edge cases:
- If threads didn’t synchronize properly, race conditions could reduce size.
- ConcurrentHashMap guarantees correctness.
- Complexity:
- put() and computeIfAbsent() average O(1).
- Total inserts ~1500 operations.
- Concurrency:
- Safe multi-threaded access without explicit locks.
- computeIfAbsent ensures mapping function executes once per key.
Real-life applications
- Maintaining thread-safe caches where values are lazily initialized.
- High-concurrency scenarios like counting, logging, or aggregating data.
- Replacement for synchronized maps when scalability is critical.
Notes
- ConcurrentHashMap distributes updates across internal buckets for parallelism.
- Initial capacity (16) grows dynamically.
- Printed order is arbitrary since ConcurrentHashMap does not guarantee iteration order.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 413b14d commit 908934c
File tree
1 file changed
+56
-0
lines changed- Section 25 Collections Frameworks/Map Interface/Concurrent HashMap/src
1 file changed
+56
-0
lines changedSection 25 Collections Frameworks/Map Interface/Concurrent HashMap/src/ConcurrentHashMapDemo1.java
Lines changed: 56 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
0 commit comments