🧵 Section 9 · 27 questions

Multithreading

27 interview questions and answers in the Multithreading section.

Questions in this section

  1. What is the difference between synchronized and volatile
  2. What is happens-before relationship
  3. What is visibility problem
  4. What is monitor in Java
  5. How does synchronized work at monitor level
  6. What is the difference between synchronized method and synchronized block
  7. What is reentrant lock
  8. What are Atomic classes
  9. What is CAS (Compare-And-Swap)
  10. How do AtomicInteger, AtomicLong work
  11. What is the advantage of Atomic classes over synchronized
  12. What is Thread Pool
  13. What types of Thread Pool exist in Java
  14. What does ExecutorService do
  15. What is the difference between Executors.newFixedThreadPool() and newCachedThreadPool()
  16. What is ForkJoinPool
  17. What is deadlock
  18. What conditions are necessary for deadlock to occur
  19. How to prevent deadlock
  20. What is race condition
  21. How to avoid race condition
  22. What are Virtual Threads in Java 21
  23. What are the advantages of Virtual Threads over regular threads
  24. When should you use Virtual Threads
  25. What is structured concurrency
  26. What is the difference between Thread and Runnable
  27. What are Callable and Future

Study navigator


📋 Table of Contents

# Question Difficulty
01 What is the difference between synchronized and volatile ⭐⭐
02 What is happens-before relationship ⭐⭐⭐
03 What is visibility problem ⭐⭐
04 What is monitor in Java ⭐⭐
05 How does synchronized work at monitor level ⭐⭐⭐
06 What is the difference between synchronized method and synchronized block ⭐⭐
07 What is reentrant lock ⭐⭐
08 What are Atomic classes ⭐⭐
09 What is CAS (Compare-And-Swap) ⭐⭐⭐
10 How do AtomicInteger, AtomicLong work ⭐⭐
11 What is the advantage of Atomic classes over synchronized ⭐⭐
12 What is Thread Pool ⭐⭐
13 What types of Thread Pool exist in Java ⭐⭐
14 What does ExecutorService do ⭐⭐
15 What is the difference between Executors.newFixedThreadPool() and newCachedThreadPool() ⭐⭐
16 What is ForkJoinPool ⭐⭐⭐
17 What is deadlock ⭐⭐
18 What conditions are necessary for deadlock to occur ⭐⭐⭐
19 How to prevent deadlock ⭐⭐⭐
20 What is race condition ⭐⭐
21 How to avoid race condition ⭐⭐
22 What are Virtual Threads in Java 21 ⭐⭐⭐
23 What are the advantages of Virtual Threads over regular threads ⭐⭐⭐
24 When should you use Virtual Threads ⭐⭐
25 What is structured concurrency ⭐⭐⭐
26 What is the difference between Thread and Runnable ⭐
27 What are Callable and Future ⭐⭐

🗺️ 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             │
                    └──────────────────────────────────────────┘

🟢 Junior Level Track

  1. Basics: What is the difference between Thread and Runnable, What are Callable and Future.
  2. Synchronization: What is the difference between synchronized and volatile, What is visibility problem.
  3. Monitors: What is monitor in Java, Method vs block synchronization.
  4. Concurrency Traps: What is deadlock, What is race condition.

🟡 Middle Level Track

  1. JMM & Memory Barriers: What is happens-before relationship.
  2. Advanced Locking: How does synchronized work at monitor level, What is reentrant lock.
  3. Lock-Free Concurrency: What are Atomic classes, What is CAS (Compare-And-Swap), How do AtomicInteger, AtomicLong work.
  4. Executors & Sizing: What is Thread Pool, What types of Thread Pool exist in Java, What does ExecutorService do, Fixed vs Cached ThreadPool.
  5. Deadlock Resolution: What conditions are necessary for deadlock to occur, How to prevent deadlock, How to avoid race condition.

🔴 Senior Level Track

  1. JVM Monitor Evolution: Thin Lock $\to$ Fat Lock, Mark Word CAS inflation, biased locking deprecation in Java 21 (Q05).
  2. Hardware Lock-Free Realities: LOCK CMPXCHG, MESI cache coherency traffic, Cache Line Bouncing, ABA problem & AtomicStampedReference (Q09, Q10).
  3. Virtual Threads Internal Architecture: Carrier threads, Continuation stack chunk unmounting, pinning hazards in synchronized blocks vs ReentrantLock (Q22, Q23, Q24).
  4. Structured Concurrency: StructuredTaskScope, cancellation propagation, short-circuiting async sub-tasks (Q25).
  5. 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).