Why Your Counter Shows 847 Instead of 1000 (Race Conditions, Explained)
java
dev.to
Two threads each increment a shared counter 1,000 times. You expect 2,000. You run it — and get 1,847. Where did the missing increments go? I animated exactly what happens below 👇 — the writeup is underneath. Why count++ isn't safe count++ looks like one operation. It's actually three: READ the current value ADD 1 WRITE it back Between any two of those steps, another thread can jump in. Both threads read the same value, both add 1, both write the same result — and one