Race condition: two or more threads access shared state concurrently and at least one writes, so the result depends on scheduling. Fixing it requires synchronization or making the access atomic.
2 / 5
What does it mean for a method to be thread-safe?
Thread-safe: the code maintains its invariants regardless of how concurrent threads interleave, typically via locks, atomic operations, or immutability. It does not require single-threaded use.
3 / 5
Why are immutable objects inherently thread-safe?
Immutability: if an object cannot be modified after creation, concurrent reads can never conflict and no synchronization is needed. This is why immutable data is favored in concurrent and functional designs.
4 / 5
What is a memory visibility problem?
Visibility: without proper synchronization (e.g., volatile or a memory barrier), a write by one thread may sit in a CPU cache and never become visible to others, causing stale reads despite no apparent race.
5 / 5
What does an atomic operation guarantee?
Atomic: the operation appears to happen all at once from other threads' perspective - no intermediate state is observable. Compare-and-swap (CAS) primitives build lock-free structures on this guarantee.