Concurrency And Multithreading Fundamentals
Asked of: Software Engineer
Last updated

What's being tested
This tests thread-safety reasoning: recognizing shared mutable state, choosing the right synchronization primitive, and avoiding races, deadlocks, and visibility bugs. Interviewers look for whether you can write correct concurrent code, explain invariants, and reason about interleavings beyond the happy path.
Patterns & templates
-
Mutex-protected critical section — guard every read/write of shared state with
Lock/synchronized; keep locked regions small and exception-safe. -
Producer-consumer queue — use
BlockingQueue,Condition, orwait/notify; handle spurious wakeups withwhile, notif. -
Read/write separation — use
ReadWriteLockwhen reads dominate writes; beware writer starvation and upgrade deadlocks. -
Atomic counters and CAS — use
AtomicInteger,compareAndSet, orfetch_addfor simple state; avoid composing multiple atomics without a lock. -
Thread pool template — submit independent tasks to
ExecutorService; bound queue size to avoid unbounded memory growth under load. -
Deadlock prevention — acquire locks in a global order; avoid calling external/user code while holding a lock.
-
Visibility guarantees — use
volatile, locks, or atomics for happens-before ordering; do not rely on timing orsleep.
Common pitfalls
Pitfall: Treating
volatileas a replacement for a lock; it gives visibility, not atomic compound operations like check-then-act.
Pitfall: Using
ifaroundwait()/Condition.await(); spurious wakeups and stolen notifications require rechecking the predicate.
Pitfall: Over-synchronizing the whole method and accidentally serializing all work, hiding correctness bugs while failing performance expectations.
Practice these
The practice cards below cover the canonical concurrency variants — solve all of them and time yourself.
Featured in interview prep guides
Related concepts
- Concurrency And Thread SafetyCoding & Algorithms
- Distributed Systems FundamentalsCoding & Algorithms
- Concurrency, Deadlocks, And SynchronizationSoftware Engineering Fundamentals
- Thread-Safe Queues And Concurrency PrimitivesCoding & Algorithms
- Concurrency Control And Thread SafetySystem Design
- Java, Concurrency, And Framework InternalsSoftware Engineering Fundamentals