Multithreading
27 interview questions and answers in the Multithreading section.
Questions in this section
- What is the difference between synchronized and volatile?
- What is happens-before relationship?
- What is visibility problem?
- What is monitor in Java?
- How does synchronized work at monitor level?
- What is the difference between synchronized method and synchronized block?
- What is reentrant lock?
- What are Atomic classes?
- What is CAS (Compare-And-Swap)?
- How do AtomicInteger, AtomicLong work?
- What is the advantage of Atomic classes over synchronized?
- What is Thread Pool?
- What types of Thread Pool exist in Java?
- What does ExecutorService do?
- What is the difference between Executors.newFixedThreadPool() and newCachedThreadPool()?
- What is ForkJoinPool?
- What is deadlock?
- What conditions are necessary for deadlock to occur?
- How to prevent deadlock?
- What is race condition?
- How to avoid race condition?
- What are Virtual Threads in Java 21?
- What are the advantages of Virtual Threads over regular threads?
- When Should You Use Virtual Threads?
- What is structured concurrency?
- What is the difference between Thread and Runnable?
- What are Callable and Future?
Study navigator
27 questions for Middle/Senior Java Developer interview preparation.
All Questions
Topic Dependency Map
┌──────────────────────────────────────────┐
│ FUNDAMENTALS (1-3) │
│ 1. synchronized vs volatile │
│ 2. happens-before │
│ 3. visibility problem │
└──────────────────┬───────────────────────┘
│
┌──────────────────────────┼──────────────────────────┐
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌────────────────────┐
│ MONITORS │ │ LOCK-FREE │ │ PROBLEMS │
│ & LOCKS (4-7) │ │ (8-11) │ │ (18-22) │
│ 4. Monitor │ │ 8. Atomic │ │ 18. Deadlock │
│ 5. synchronized│ │ 9. CAS │ │ 19. Coffman cond. │
│ internals │ │ 10. AtomicInteger│ │ 20. Prevention │
│ 6. method vs │ │ AtomicLong │ │ 21. Race condition │
│ block │ │ 11. vs sync │ │ 22. Avoidance │
│ 7. Reentrant │ │ │ │ │
│ Lock │ │ │ │ │
└───────┬───────┘ └───────┬───────┘ └────────┬───────────┘
│ │ │
└────────────────────────┼────────────────────────┘
▼
┌──────────────────────────────────────────┐
│ THREAD POOLS (12-17) │
│ 12. Thread Pool (concept) │
│ 13. Pool types │
│ 15. ExecutorService │
│ 16. Fixed vs Cached │
│ 17. ForkJoinPool │
└──────────────────────────────────────────┘
│
┌────────────────────────┼────────────────────────┐
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌────────────────────┐
│ JAVA 21+ │ │ BASICS │ │ │
│ (23-26) │ │ (27-28) │ │ │
│ 23. Virtual │ │ 27. Thread vs │ │ │
│ Threads │ │ Runnable │ │ │
│ 24. Advantages │ │ 28. Callable │ │ │
│ 25. When to │ │ & Future │ │ │
│ use │ │ │ │ │
│ 26. Structured │ │ │ │ │
│ Concurrency│ │ │ │ │
└───────────────┘ └───────────────┘ └────────────────────┘
Recommended Study Order
Junior Level (weeks 1-2)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Basics | Q1, Q3 | synchronized vs volatile, visibility problem |
| 2 | Monitors | Q4, Q6 | What is a monitor, method vs block |
| 3 | Thread & Runnable | Q27, Q28 | Thread vs Runnable, Callable & Future |
| 4 | Deadlock basics | Q18 | What it is, examples |
| 5 | Race condition basics | Q21, Q22 | What it is, how to avoid |
Middle Level (weeks 3-4)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | happens-before | Q2 | 8 rules of JMM, program order, volatile, start/join |
| 2 | Monitors deep dive | Q5 | State evolution, thin/fat lock, JIT |
| 3 | ReentrantLock | Q7 | AQS, fair vs non-fair, Condition |
| 4 | CAS & Atomic | Q8, Q9, Q10 | CAS cycle, ABA, VarHandle, Atomic* |
| 5 | Thread Pools | Q12, Q13, Q15, Q16 | Pool types, ExecutorService, sizing |
| 6 | Deadlock deep dive | Q19, Q20 | Coffman conditions, lock ordering, tryLock |
| 7 | ForkJoinPool | Q17 | Work-stealing, recursive tasks |
Senior Level (weeks 5-6)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | synchronized internals | Q5 (Senior) | Mark Word, CAS spin, inflation, elision |
| 2 | CAS internals | Q9 (Senior) | lock cmpxchg, memory barriers, progress guarantees |
| 3 | Virtual Threads | Q23, Q24, Q25 | StackChunk, carrier threads, pinning, unmount |
| 4 | Structured Concurrency | Q26 | ShutdownOnFailure/Success, scope lifetime |
| 5 | Atomic vs Lock perf | Q11 (Senior) | Break-even point, contention curves |
| 6 | Race conditions | Q21, Q22 (Senior) | Heisenbugs, safe publication, ThreadLocal |
| 7 | ThreadPool tuning | Q12-Q16 (Senior) | Sizing formulas, rejection policies, monitoring |
Key Topic Connections
Topic: Synchronization
Q1 (synchronized vs volatile) → Q4 (Monitor) → Q5 (synchronized internals)
↓ ↓ ↓
Q3 (Visibility) Q6 (method vs block) Q7 (ReentrantLock)
Key connections:
- Q1 ↔ Q3: volatile solves visibility problem, but NOT atomicity
- Q4 ↔ Q5: monitor is the concept, synchronized is the implementation
- Q5 ↔ Q7: synchronized → ReentrantLock (evolution from language-level to library-level)
- Q6 ↔ Q7: synchronized block = ReentrantLock.lock/unlock
Topic: Lock-Free
Q9 (CAS) → Q8 (Atomic classes) → Q10 (AtomicInteger/Long)
↓ ↓
└──────────────────────→ Q11 (vs synchronized)
Key connections:
- Q9 ↔ Q10: AtomicInteger = wrapper over CAS
- Q8 ↔ Q11: Atomic vs synchronized — optimistic vs pessimistic approach
- Q9 ↔ Q18: CAS prevents deadlock (no blocking), but not race conditions
Topic: Multithreading Problems
Q18 (Deadlock) → Q19 (Coffman conditions) → Q20 (Prevention)
Q21 (Race condition) → Q22 (Avoidance)
Key connections:
- Q18 ↔ Q21: Deadlock = threads waiting for each other, Race = result depends on order
- Q19 ↔ Q20: Break one Coffman condition → deadlock becomes impossible
- Q21 ↔ Q3: Race conditions are often caused by visibility problems
Topic: Thread Pools
Q12 (Thread Pool) → Q13 (Types) → Q15 (ExecutorService)
↓ ↓ ↓
Q16 (Fixed vs Cached) Q17 (ForkJoin) Q16 (comparison)
Key connections:
- Q12 ↔ Q13: Pool concept → concrete implementations
- Q15 ↔ Q16: ExecutorService → concrete factories (Fixed, Cached)
- Q17 ↔ Q13: ForkJoinPool = WorkStealingPool for divide-and-conquer
Topic: Java 21+ (The Future)
Q23 (Virtual Threads) → Q24 (Advantages) → Q25 (When to use)
↓
Q26 (Structured Concurrency)
Key connections:
- Q23 ↔ Q24: VT architecture → why cheaper than platform threads
- Q23 ↔ Q13: VT replaces CachedThreadPool for I/O-bound tasks
- Q25 ↔ Q12: VT does NOT replace Thread Pool for CPU-bound tasks
- Q26 ↔ Q15: StructuredTaskScope replaces ExecutorService for structured tasks
Cheat Sheet: What to Know at Each Level
Junior
- synchronized = locking, volatile = visibility
- Thread created via Runnable (composition, not inheritance)
- Callable returns a result, Runnable does not
- Deadlock = two threads waiting for each other forever
- Race condition = result depends on thread execution order
- Thread Pool reuses threads, saves memory and time
Middle
- happens-before: program order, volatile, monitor, start/join, transitivity
- Monitor: No Lock → Thin Lock → Fat Lock, Mark Word, ObjectMonitor
- ReentrantLock: AQS, fair vs non-fair, Condition (wait/notify analogue)
- CAS: optimistic approach, retry cycle, ABA problem → AtomicStampedReference
- Thread Pool: Fixed (known load), Cached (short tasks), Scheduled (periodic)
- Deadlock prevention: lock ordering, tryLock(timeout), single lock
- AtomicInteger: CAS cycle, lazySet, LongAdder for high contention
Senior
- synchronized internals: biased (removed in Java 21), thin (CAS spin), fat (OS park)
- JIT: lock elision (escape analysis), lock coarsening
- CAS: lock cmpxchg (x86), memory barriers, progress guarantees (wait-free > lock-free > obstruction-free)
- Virtual Threads: StackChunk, carrier threads, unmount on I/O, pinning (synchronized blocks carrier!)
- Structured Concurrency: ShutdownOnFailure/Success, scope lifetime, preview API
- ThreadPool sizing: CPU-bound = cores + 1, I/O-bound = cores × (1 + W/S)
- ForkJoinPool: work-stealing deque, LIFO for owner/FIFO for thief, commonPool dangers
File Format
Each file contains:
- Junior Level — basic understanding, simple analogies, examples
- Middle Level — internals, common mistakes, practical examples
- Senior Level — deep dive, edge cases, production experience, monitoring
- Interview Cheat Sheet — key points, frequent questions, red flags, related topics