Medium👤 3-5 years👤 8-15 years 1 min read

What is a check-then-act race condition, and how do you fix it correctly?

Asked inAmazonGoogleDeloitteCognizant
#race condition#check-then-act#atomicity#putifabsent
Report issue

⚡ 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.

Coffee Chat Question

Concept Made Simple

What is a check-then-act race condition, and how do you fix it correctly?

🧠Mind Map Answer

Remember It Faster

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.

⌨️Hands-on Keyboard

Learn by Doing

java
// BAD: check-then-act race
if (!cache.containsKey(k)) cache.put(k, load(k));

// GOOD: atomic
cache.computeIfAbsent(k, this::load);

🔥What If?

Think Beyond the Expected

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 atomictwo threads pass the checkputIfAbsent/computeIfAbsent/compareAndSetlost 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

  • 1Why isn't synchronizedMap enough for check-then-act?
  • 2How does computeIfAbsent guarantee single execution?
  • 3How do DB unique constraints act as a safety net?

🧩Related Technologies

ConcurrentHashMapAtomicReference.compareAndSetDB unique index

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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions