Skip to content

Commit 413b14d

Browse files
committed
feat(ConcurrentHashMapDemo): add minimal example with detailed internal notes
What - Added ConcurrentHashMapDemo class. - Created a ConcurrentHashMap<String, Integer>. - Inserted two key-value pairs ("World" → 2, "Hello" → 1). - Printed map contents. Why - Demonstrates how to use ConcurrentHashMap with a minimal example. - Preserves original simple logic while adding educational documentation. - Explains internal implementation differences between Java 7 and Java 8+. How - map.put() safely adds entries, supporting concurrency without explicit synchronization. - System.out.println(map) prints current contents (unordered). - Documentation added to describe segment-based locking (Java 7) vs CAS-based design (Java 8). Logic - Input: none (map entries hardcoded). - Processing: thread-safe puts into the ConcurrentHashMap. - Output: printed contents of the map. - Order of keys is non-deterministic because ConcurrentHashMap does not guarantee ordering. Real-life applications - Thread-safe caches. - Shared counters or statistics maps. - Concurrent access to maps without needing synchronized blocks. Notes - Use ConcurrentHashMap when multiple threads read/write frequently. - Not ordered: if you need order, use ConcurrentSkipListMap instead. - Avoid storing null keys or values (not supported in ConcurrentHashMap). Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 75ed329 commit 413b14d

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.concurrent.ConcurrentHashMap;
2+
3+
/**
4+
* Demonstrates the basic usage of {@link ConcurrentHashMap} with the exact
5+
* logic you had originally.
6+
*
7+
* <p>
8+
* This class contains a short example showing two puts and a println of the map.
9+
* The example is intentionally minimal to keep the logic unchanged while providing
10+
* detailed documentation about the internal locking/implementation evolution:
11+
* </p>
12+
*
13+
* <ul>
14+
* <li><b>Java 7:</b> ConcurrentHashMap used segment-based locking (multiple segments,
15+
* each a small hash table). Writes locked only the affected segment.</li>
16+
* <li><b>Java 8:</b> Implementation moved to a lock-free / CAS-based design for most
17+
* operations (still needs synchronization for table resize and some collision cases).
18+
* This yields better concurrency and throughput.</li>
19+
* </ul>
20+
*/
21+
22+
public class ConcurrentHashMapDemo {
23+
public static void main(String[] args) {
24+
// Create a ConcurrentHashMap (thread-safe map implementation).
25+
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
26+
27+
/*
28+
* Put two entries into the map.
29+
* NOTE: This logic is kept exactly as in the original snippet (no changes).
30+
*/
31+
map.put("World", 2);
32+
map.put("Hello", 1);
33+
34+
/*
35+
* Print the current map contents to standard output.
36+
* The printed order is not guaranteed; ConcurrentHashMap is not ordered.
37+
*/
38+
System.out.println(map);
39+
}
40+
}

0 commit comments

Comments
 (0)