What is a check-then-act race condition, and how do you fix it correctly?
Reviewed by Gurusankar M.
Short Answer
Check-then-act (e.g. 'if not present, put') is two steps that aren't atomic — two threads both pass the check and both act, causing duplicates/lost updates. Fix with an atomic compound operation (putIfAbsent, computeIfAbsent, compareAndSet) or a lock.
On this page
Concept Made Simple
Coffee Chat Question
“What is a check-then-act race condition, and how do you fix it correctly?”
Remember It Faster
Mind Map Answer
if (!map.containsKey(k)) map.put(k, v); — two threads can both see 'absent' and both put. The check and the act must be one atomic step.
Learn by Doing
Hands-on Keyboard
// BAD: check-then-act race
if (!cache.containsKey(k)) cache.put(k, load(k));
// GOOD: atomic
cache.computeIfAbsent(k, this::load);Think Beyond the Expected
What If?
A lazy-init cache occasionally loads the same key twice under load — root cause?
Classic check-then-act race: two threads find the key absent and both call the expensive load(). computeIfAbsent makes the check-and-insert atomic per key, so load() runs once — and on ConcurrentHashMap it's done under the bin lock.
Real World
Duplicate inserts, double-charging, and 'singleton created twice' bugs are almost always non-atomic check-then-act; the fix is an atomic method or a unique DB constraint as a backstop.
Interviewer's Expectation
Keywords they're listening for:
- compound op not atomic
- two threads pass the check
- putIfAbsent/computeIfAbsent/compareAndSet
- lost update/duplicate
Common Mistakes
- containsKey then put on a shared map
- get-then-put for increments
- Assuming synchronizedMap makes compound ops atomic
Best Practices
- Use atomic compound operations
- Back critical invariants with DB constraints
- Make lazy init idempotent
Follow-up Questions
- Why isn't synchronizedMap enough for check-then-act?
- How does computeIfAbsent guarantee single execution?
- How do DB unique constraints act as a safety net?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: volatile & Memory Model (Multithreading) Interview question: "What is a check-then-act race condition, and how do you fix it correctly?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair