Race condition
A race condition is a bug whose result depends on the unpredictable timing of threads. Two threads read and write the same data without a happens-before edge or an atomic operation, and the answer is wrong only sometimes.
Detailed Explanation
count++ on a shared int is the smallest race. Each increment is a read, an add, and a write. Two threads can both read 7 and both write 8. You expected 9. The program is legal Java; the interleaving is not what you meant.
Check-then-act is the other shape. if (!map.containsKey(k)) map.put(k, v) looks safe and is not: another thread can insert between the two calls. putIfAbsent is the atomic version. Read-then-write on a HashMap from two threads is worse than a lost increment: the table can corrupt.
Visibility races are quieter. Thread A sets ready = true without volatile or a lock. Thread B loops on ready and never sees the write. That is still a race: the outcome depends on caches and compiler reordering.
A data race in the JMM sense is unsynchronized conflicting access to a non-volatile field. Not every race condition is a data race (two get+put on a concurrent map can still be a logical race). Every data race is undefined behaviour for that field.
You find races with thought, with jcstress or TLA-style reasoning for hard cases, and with the thread sanitizer is not a Java default. Tests that "sleep a bit" pass on a laptop and fail in CI. Fix the shared state: one lock, an atomic, a concurrent collection, or no sharing (thread confinement, immutable messages).
You reach for this word when a bug is intermittent and involves mutable shared data. You prevent it by shrinking what is shared, making updates atomic at the right grain, and documenting who may write.
The cost of a benign-looking race is a security hole (check then use) or a lost update in money. The cost of synchronizing everything is speed. Protect the invariant, not every line.
Code Examples
import java.util.concurrent.atomic.AtomicInteger;
public class LostUpdate {
static int unsafe;
static final AtomicInteger safe = new AtomicInteger();
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
for (int i = 0; i < 100_000; i++) {
unsafe++;
safe.incrementAndGet();
}
});
t.start();
for (int i = 0; i < 100_000; i++) {
unsafe++;
safe.incrementAndGet();
}
t.join();
System.out.println(safe.get()); // prints: 200000
System.out.println(unsafe != 200_000 || unsafe == 200_000);
// unsafe is often not 200000; the second print is always true
}
}import java.util.concurrent.ConcurrentHashMap;
public class CheckThenAct {
public static void main(String[] args) {
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
map.putIfAbsent("n", 1); // atomic, not contains + put
System.out.println(map.get("n")); // prints: 1
}
}Common Gotchas
Tests with `Thread.sleep` do not prove safety
they hide races until a different machine.
`ConcurrentHashMap` does not make your multi-step business logic atomic
use `compute`/`merge`.
A visibility race can look like an infinite loop
the flag never appears to change.
"It works on my machine" is the race-condition slogan
"It works on my machine" is the race-condition slogan