Commit d441b81
committed
docs(concurrency): add detailed comparison of volatile and AtomicInteger in Java
What
- Added documentation explaining differences between `volatile` and `AtomicInteger` in multithreaded programming.
- Covered:
1. **Volatile**
- Guarantees visibility (reads/writes go to main memory).
- Does not guarantee atomicity.
- Example: `volatile int count = 0;` → `count++` is not atomic and can lead to lost updates.
2. **AtomicInteger**
- Provides atomic operations (`incrementAndGet()`, `compareAndSet()`).
- Lock-free, CAS-based, ensuring both visibility and atomicity.
- Example: `AtomicInteger count = new AtomicInteger(0);` → `count.incrementAndGet()` ensures safe increments.
3. **When to Use**
- Use `volatile` for flags (simple read/write state variables).
- Use `AtomicInteger` for counters or shared mutable integers requiring atomic updates.
Why
- Clarifies a common misconception: that `volatile` alone ensures thread-safety.
- Highlights that atomicity requires `AtomicInteger` (or synchronization), not just visibility.
- Provides practical guidance on choosing between the two.
Logic
- **Volatile semantics**: read/write operations are always visible to all threads immediately, but compound actions like `count++` are still unsafe because they break into multiple non-atomic steps (read → increment → write).
- **AtomicInteger semantics**: all key operations (increment, compare-and-swap) are atomic, leveraging CPU-level CAS instructions. No race conditions occur.
- **Decision point**:
- Use `volatile` when you only need a one-way signal or visibility guarantee.
- Use `AtomicInteger` when multiple threads must safely modify a shared counter without locking.
Key Takeaways
✔ `volatile` → visibility only, not atomicity.
✔ `count++` with `volatile` can lose updates under concurrency.
✔ `AtomicInteger` → provides lock-free atomic operations with visibility.
✔ Ideal for counters, accumulators, and CAS logic.
✔ Use `volatile` for flags (boolean control variables), not for shared mutable counters.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 8180e70 commit d441b81
File tree
2 files changed
+69
-37
lines changed- JAVA8
- Java 8 Crash Course/Java 8 Streams/volatile vs AtomicInteger/src
2 files changed
+69
-37
lines changedThis file was deleted.
Lines changed: 69 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 | + | |
| 68 | + | |
| 69 | + | |
0 commit comments