Loading lesson…
Level 1 · Apprentice0 XP
What goes wrong when two threads touch the same data, and the tools that fix it.
Step 1 / 5
ProblemTwo threads each add 1 to a shared counter 100,000 times. The result should be 200,000. It is 137,482, and different on every run. No exception, no warning.
← → step
Before this: Annotations and reflection
🛠️ Your turn
Reading is not the skill, doing is. Try for ten minutes before you open the hint. Getting stuck first is what makes the solution stick.
1.Reproduce the lost update: two threads each increment a plain int field 100,000 times and print the result a few times. Fix it three ways (synchronized, AtomicInteger, and summing two local counts) and confirm each prints 200,000.