MediumExperience level: 3-5 years · 8-15 years1 min read

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

Reviewed by Gurusankar M.

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.

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

java
// 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

  1. Why isn't synchronizedMap enough for check-then-act?
  2. How does computeIfAbsent guarantee single execution?
  3. 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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.