Commit a12e011
committed
feat: implement thread-safe counter using AtomicInteger vs volatile
WHAT was added:
- A class `VolatileCounter2` demonstrating difference between `volatile` and `AtomicInteger`.
- Two worker threads increment a shared counter 1000 times each.
- Shows how `AtomicInteger.incrementAndGet()` ensures atomicity and prevents lost updates.
- Prints final counter value after both threads complete.
WHY this matters:
- `volatile` only guarantees visibility (all threads see latest value),
but it does not make compound operations (like `counter++`) atomic.
- With plain `volatile`, increments can be lost under race conditions.
- `AtomicInteger` provides atomic methods (`incrementAndGet`, `getAndAdd`, etc.)
using low-level Compare-And-Swap (CAS), ensuring correctness without locks.
- This example highlights when to use `volatile` vs when to prefer `AtomicInteger`.
HOW it works:
1. Shared `AtomicInteger counter` initialized to 0.
2. Two threads concurrently call `vc2.increment()` 1000 times each.
3. `incrementAndGet()` ensures thread-safe increments.
4. After both threads join, final result is `2000` (no lost updates).
5. Contrast: with plain `volatile int counter`, result could be less than 2000 due to race conditions.
Key Concepts:
- Visibility vs atomicity: `volatile` ensures visibility, `AtomicInteger` ensures both visibility + atomicity.
- CAS operation: retries until update succeeds (non-blocking).
- Lightweight alternative to `synchronized` or `ReentrantLock`.
REAL-LIFE APPLICATIONS:
- Web servers → counting number of requests served in real time safely across multiple threads.
- Rate limiting → tracking API calls per user without race conditions.
- Monitoring systems → counting events/logs without locking overhead.
- Thread-safe statistics → updating counters in analytics pipelines.
- Producer-consumer systems → maintaining counters for produced vs consumed messages.
LIMITATIONS:
- `AtomicInteger` works for simple numeric counters.
- For more complex atomic updates (multiple variables), use locks or `AtomicReference`.
- Busy CAS retry loops may cause performance overhead under very high contention.
KEYWORDS:
volatile atomic integer concurrency race-condition CAS compare-and-swap thread-safe counter.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent c8923e2 commit a12e011
File tree
1 file changed
+67
-0
lines changed- Section19MultiThreading/Volatile and Atomic/src
1 file changed
+67
-0
lines changedLines changed: 67 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 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
0 commit comments