Section 9 · 27 questions

Multithreading

27 interview questions and answers in the Multithreading section.

English Multithreading Source Markdown
Language versions: English Russian Ukrainian

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

27 questions for Middle/Senior Java Developer interview preparation.


All Questions

# Question Difficulty
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 ⭐⭐
15 What does ExecutorService do ⭐⭐
16 What is the difference between Executors.newFixedThreadPool() and newCachedThreadPool() ⭐⭐
17 What is ForkJoinPool ⭐⭐⭐
18 What is deadlock ⭐⭐
19 What conditions are necessary for deadlock ⭐⭐⭐
20 How to prevent deadlock ⭐⭐⭐
21 What is race condition ⭐⭐
22 How to avoid race condition ⭐⭐
23 What are Virtual Threads in Java 21 ⭐⭐⭐
24 What are the advantages of Virtual Threads over regular threads ⭐⭐⭐
25 When to use Virtual Threads ⭐⭐
26 What is structured concurrency ⭐⭐⭐
27 What is the difference between Thread and Runnable
28 What are Callable and Future ⭐⭐

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

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