Loading lesson…
Level 1 · Apprentice0 XP
Why count++ loses updates, and how one special instruction fixes it.
Step 1 / 6
ProblemIn Java's 'Threads and shared state' you fixed a lost counter (137,412 instead of 200,000) with synchronized or with AtomicInteger. Both are correct, but AtomicInteger is often faster, and it never puts a thread to sleep. Why? The answer is one special CPU instruction.
← → step
Before this: What a thread really is
🛠️ 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.Fix the counter with AtomicInteger, then write incrementByHand with compareAndSet, and count how many times the retry loop actually retries under 4 threads.