🧵 Section 9 · 27 questions
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
📋 Table of Contents
🗺️ Topic Dependency Map
┌──────────────────────────────────────────┐
│ FUNDAMENTALS (Q01-Q03) │
│ 01. synchronized vs volatile │
│ 02. happens-before relationship │
│ 03. visibility problem │
└──────────────────┬───────────────────────┘
│
┌──────────────────────────┼──────────────────────────┐
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌────────────────────┐
│ MONITORS │ │ LOCK-FREE │ │ PROBLEMS │
│ & LOCKS │ │ CONCURRENCY │ │ (Q17-Q21) │
│ (Q04-Q07) │ │ (Q08-Q11) │ │ 17. Deadlock │
│ 04. Monitor │ │ 08. Atomic cls│ │ 18. Coffman conds │
│ 05. sync inter│ │ 09. CAS │ │ 19. Prevention │
│ 06. meth vs bl│ │ 10. AtomicInt │ │ 20. Race condition │
│ 07. Reentrant │ │ 11. vs sync │ │ 21. Avoidance │
└───────┬───────┘ └───────┬───────┘ └────────┬───────────┘
│ │ │
└────────────────────────┼────────────────────────┘
▼
┌──────────────────────────────────────────┐
│ THREAD POOLS & ASYNC (Q12-Q16, Q26-Q27)│
│ 12. Thread Pool Concepts │
│ 13. Thread Pool Types │
│ 14. ExecutorService │
│ 15. Fixed vs Cached ThreadPool │
│ 16. ForkJoinPool & Work-Stealing │
│ 26. Thread vs Runnable │
│ 27. Callable & Future │
└──────────────────┬───────────────────────┘
│
▼
┌──────────────────────────────────────────┐
│ MODERN JAVA 21+ (Q22-Q25) │
│ 22. Virtual Threads Architecture │
│ 23. Advantages of Virtual Threads │
│ 24. When to Use Virtual Threads │
│ 25. Structured Concurrency │
└──────────────────────────────────────────┘
🎯 Recommended Learning Paths
🟢 Junior Level Track
- Basics: What is the difference between Thread and Runnable, What are Callable and Future.
- Synchronization: What is the difference between synchronized and volatile, What is visibility problem.
- Monitors: What is monitor in Java, Method vs block synchronization.
- Concurrency Traps: What is deadlock, What is race condition.
🟡 Middle Level Track
- JMM & Memory Barriers: What is happens-before relationship.
- Advanced Locking: How does synchronized work at monitor level, What is reentrant lock.
- Lock-Free Concurrency: What are Atomic classes, What is CAS (Compare-And-Swap), How do AtomicInteger, AtomicLong work.
- Executors & Sizing: What is Thread Pool, What types of Thread Pool exist in Java, What does ExecutorService do, Fixed vs Cached ThreadPool.
- Deadlock Resolution: What conditions are necessary for deadlock to occur, How to prevent deadlock, How to avoid race condition.
🔴 Senior Level Track
- JVM Monitor Evolution: Thin Lock $\to$ Fat Lock, Mark Word CAS inflation, biased locking deprecation in Java 21 (Q05).
- Hardware Lock-Free Realities:
LOCK CMPXCHG, MESI cache coherency traffic, Cache Line Bouncing, ABA problem &AtomicStampedReference(Q09, Q10). - Virtual Threads Internal Architecture: Carrier threads,
Continuationstack chunk unmounting, pinning hazards insynchronizedblocks vsReentrantLock(Q22, Q23, Q24). - Structured Concurrency:
StructuredTaskScope, cancellation propagation, short-circuiting async sub-tasks (Q25). - Thread Pool Tuning Formulas: Sizing CPU-bound ($N_{\text{threads}} = N_{\text{cores}} + 1$) vs I/O-bound ($N_{\text{threads}} = N_{\text{cores}} \times (1 + \frac{W}{C})$), rejection policies, saturation monitoring (Q12–Q16).