Commit b16de06
committed
feat: add thread-safe counter using AtomicInteger to ensure atomicity
WHAT was added:
- Implemented `VolatileCounter` class with a shared `AtomicInteger counter`.
- Two threads increment the counter 1000 times each.
- Final value is guaranteed to be 2000 due to atomic increments.
- Demonstrates why `volatile` alone is not sufficient for compound operations like counter++.
WHY this matters:
- Concurrency introduces race conditions when multiple threads update shared variables.
- `volatile` ensures visibility (threads see the latest value) but does not ensure atomicity.
Example: counter++ = read + add + write (3 steps). These can interleave and cause lost updates.
- `AtomicInteger` provides atomic operations (`incrementAndGet`, `getAndAdd`) using Compare-And-Swap (CAS).
- CAS is lock-free and more efficient than `synchronized` for simple counters.
HOW it works:
1. `counter` is an `AtomicInteger` initialized to 0.
2. Two threads call `vc.increment()` 1000 times each.
3. `incrementAndGet()` ensures the entire increment is atomic.
4. After `join()`, final output is always 2000.
5. Contrast:
- With plain `volatile int counter`, final result < 2000 (due to lost updates).
- With `synchronized`, result = 2000 but at the cost of heavier locking.
REAL-LIFE APPLICATIONS:
- Web servers → counting active requests or total hits safely across threads.
- Logging systems → counting error/warning occurrences without locks.
- Metrics & monitoring → real-time counters in Prometheus or similar systems.
- Banking apps → tracking transactions or balance updates safely.
- Producer-consumer queues → keeping track of queue size in multithreaded systems.
LIMITATIONS:
- `AtomicInteger` only solves atomicity for a single variable.
- For multiple related variables (e.g., debit & credit together), atomic classes are not enough — use locks or higher-level constructs.
- High contention can reduce CAS efficiency, though still generally faster than synchronization.
KEYWORDS:
volatile vs synchronized vs AtomicInteger, atomicity, CAS (Compare-And-Swap), lock-free concurrency, thread-safe counter.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent a12e011 commit b16de06
File tree
1 file changed
+11
-24
lines changed- Section19MultiThreading/Volatile and Atomic/src
1 file changed
+11
-24
lines changedLines changed: 11 additions & 24 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | 8 | | |
17 | 9 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
| 10 | + | |
29 | 11 | | |
30 | | - | |
31 | | - | |
32 | 12 | | |
33 | 13 | | |
34 | 14 | | |
| |||
47 | 27 | | |
48 | 28 | | |
49 | 29 | | |
50 | | - | |
51 | 30 | | |
52 | 31 | | |
53 | | - | |
54 | 32 | | |
55 | 33 | | |
56 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
0 commit comments