Race condition
Two or more threads read and write shared state without a stable coordination rule.
Threading bugs are expensive because they are intermittent, environment-sensitive, and often invisible in small tests. The same code may pass ten times and fail on the eleventh run under slightly different timing. This page focuses on the bug families teams actually hit in Java systems and the design habits that reduce them.
If inventory reservation runs concurrently with cancellation or refund logic, stale reads and races can produce overselling or invalid order states. These failures are rarely caused by one dramatic line of code. They come from unclear ownership, weak visibility guarantees, and hidden timing assumptions.
The most frequent bug families are race conditions, deadlocks, visibility errors, lost updates, starvation, and misuse of thread pools or blocking calls. Many teams treat them as low-level implementation mistakes, but they are usually design mistakes first. The architecture allowed multiple actors to mutate the same truth without a clear protocol.
A good concurrency design reduces the number of places where timing can matter. It narrows shared mutable state, makes coordination explicit, and keeps thread ownership understandable.
Two or more threads read and write shared state without a stable coordination rule.
Two paths hold resources in conflicting order and wait forever.
One thread updates state and another never sees it reliably because the memory relationship is weak.
Two threads compute from the same old value and one write silently overwrites the other.
The work is logically possible, but the scheduling and locking strategy prevents progress.
One thread sees an out-of-date view of state and makes a decision that is no longer valid.
Blocking work consumes the same executor needed for progress, so throughput collapses.
package org.javaomnibus.inventory;
public final class StockCounter {
private int available = 10;
public void reserveOne() {
if (available > 0) {
available = available - 1;
}
}
}