Skip to content

Commit 6c67bd3

Browse files
committed
feat(ThreadSafeHashTable): demonstrate synchronized thread-safe operations in Hashtable
What - Added `ThreadSafeHashTable` class to showcase how `Hashtable` ensures thread safety with multiple writers. - Spawned two threads: - Thread1 inserts keys 0 → 1000 with value `"New Thread1 Running"`. - Thread2 inserts keys 1000 → 1999 with value `"Thread2"`. - Waited for both threads (`join()`). - Printed final size of the map (`2000` entries). Why - To illustrate that unlike `HashMap`, all methods of `Hashtable` are synchronized. - Validates safe concurrent access without external synchronization. - Highlights the trade-off: thread safety but slower performance compared to non-synchronized collections. How - Step 1: Create a `Hashtable<Integer,String>` instance. - Step 2: Define first thread to `put` entries for keys 0–1000. - Step 3: Define second thread to `put` entries for keys 1000–1999. - Step 4: Start both threads and `join()` to ensure completion. - Step 5: Print `map.size()`, expected `2000`. Logic - Inputs: hard-coded key ranges (0–1999). - Outputs: `"Final size of HashTable: 2000"`. - Flow: 1. Instantiate `Hashtable`. 2. Launch two concurrent writers. 3. Synchronization in `Hashtable` ensures correctness. 4. Join threads. 5. Print map size. Real-life applications - Legacy multi-threaded applications before `ConcurrentHashMap`. - Small-scale shared maps where built-in synchronization is sufficient. - Teaching concurrency safety basics in Java collections. Notes - `Hashtable` synchronizes all operations (method-level). - This avoids corruption but limits scalability under high contention. - For modern concurrent workloads, prefer `ConcurrentHashMap`. - Demonstrates the classic synchronized map pattern and its impact. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent dfb58bd commit 6c67bd3

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.util.Hashtable;
2+
3+
/**
4+
* Demonstrates thread-safety in {@link Hashtable}.
5+
*
6+
* <p>
7+
* Key Points:
8+
* <ul>
9+
* <li>Hashtable is synchronized → all its methods (including get/put) are thread-safe.</li>
10+
* <li>Multiple threads can update the Hashtable concurrently without corrupting data.</li>
11+
* <li>However, synchronization can cause performance overhead compared to non-synchronized maps like HashMap.</li>
12+
* </ul>
13+
*
14+
* @author Somesh Diwan
15+
*/
16+
17+
public class ThreadSafeHashTable {
18+
public static void main(String[] args) {
19+
/*
20+
Concurrency in Hashtable:
21+
- Unlike HashMap, Hashtable is thread-safe.
22+
- Every method (including get() and put()) is synchronized.
23+
- This ensures safe usage across multiple threads but makes it slower in single-threaded use cases.
24+
*/
25+
Hashtable<Integer, String> map = new Hashtable<>();
26+
27+
// The first thread inserts keys from 0 to 1000
28+
Thread newThread1 = new Thread(() -> {
29+
for (int i = 0; i <= 1000; i++) {
30+
map.put(i, "New Thread1 Running");
31+
}
32+
});
33+
34+
// The second thread inserted keys from 1000 to 1999
35+
Thread thread2 = new Thread(() -> {
36+
for (int i = 1000; i < 2000; i++) {
37+
map.put(i, "Thread2");
38+
}
39+
});
40+
41+
// Start both threads
42+
newThread1.start();
43+
thread2.start();
44+
45+
// Wait for both threads to complete
46+
try {
47+
newThread1.join();
48+
thread2.join();
49+
} catch (InterruptedException e) {
50+
e.printStackTrace();
51+
}
52+
53+
// The final size will be 2000 (keys 0–1999) — proving thread safety.
54+
System.out.println("Final size of HashTable: " + map.size());
55+
}
56+
}

0 commit comments

Comments
 (0)