DevNotes
Support
Java · 19 chapters

Multithreading

Java concurrency from first principles to production: thread lifecycle, race conditions, synchronized, the Java Memory Model and volatile, wait/notify, deadlocks, explicit locks, atomics and CAS, executors and ThreadPoolExecutor sizing, CompletableFuture, synchronizers, concurrent collections, ThreadLocal, Fork/Join, virtual threads and Spring @Async.

CHAPTER 01

Introduction to Multithreading

Processes vs threads, concurrency vs parallelism, why backends use threads, what threads share, and the costs and risks that the rest of this topic teaches you to control.

7 min · 6 Q&A · 1 examples
CHAPTER 02

Creating Threads

Extending Thread vs implementing Runnable, lambdas, start() vs run(), naming, daemon threads, priorities, uncaught-exception handlers, and why production code uses executors instead.

7 min · 7 Q&A · 12 examples
CHAPTER 03

Thread Lifecycle & Thread Methods

The six thread states, sleep(), join(), yield(), interrupt() and cooperative cancellation, sleep vs wait, and reading states in a thread dump.

7 min · 7 Q&A · 6 examples
CHAPTER 04

Race Conditions & synchronized

Shared mutable state, why count++ isn't atomic, critical sections, synchronized methods and blocks, object vs class locks, reentrancy, and the hidden race in Spring singleton beans.

9 min · 7 Q&A · 10 examples
CHAPTER 05

Java Memory Model & volatile

Visibility and reordering problems, CPU caches, happens-before, what volatile does and doesn't guarantee, volatile vs synchronized, safe publication and double-checked locking.

9 min · 6 Q&A · 9 examples
CHAPTER 06

Inter-Thread Communication — wait & notify

Guarded blocks, wait(), notify() and notifyAll(), why wait() must be in a while loop, spurious wakeups, a bounded producer–consumer buffer, and the modern alternatives.

7 min · 7 Q&A · 5 examples
CHAPTER 07

Deadlock, Livelock & Starvation

How deadlocks form, the four Coffman conditions, lock ordering, tryLock with timeout and other prevention strategies, detecting deadlocks with thread dumps, and livelock and starvation.

9 min · 6 Q&A · 7 examples
CHAPTER 08

Explicit Locks — ReentrantLock, ReadWriteLock & StampedLock

The Lock interface, lock/unlock in try-finally, tryLock and lockInterruptibly, fairness, Condition variables, ReentrantReadWriteLock, StampedLock optimistic reads, and synchronized vs ReentrantLock.

8 min · 7 Q&A · 7 examples
CHAPTER 09

Atomic Variables & Compare-and-Swap

AtomicInteger, AtomicLong and AtomicReference, how CAS works, lock-free algorithms, updateAndGet, the ABA problem, LongAdder for high contention, and when atomics aren't enough.

7 min · 6 Q&A · 8 examples
CHAPTER 10

The Executor Framework

Executor and ExecutorService, the Executors factory methods and their hidden risks, execute vs submit, Callable and Future, invokeAll and invokeAny, and shutting a pool down correctly.

8 min · 7 Q&A · 8 examples
CHAPTER 11

ThreadPoolExecutor Internals & Pool Sizing

Core and maximum pool size, work queues, keep-alive, the task-acceptance algorithm, rejection policies, custom ThreadFactory, sizing pools for CPU- and I/O-bound work, and monitoring.

7 min · 6 Q&A · 4 examples
CHAPTER 12

CompletableFuture

Asynchronous pipelines without blocking — supplyAsync, thenApply vs thenCompose, thenCombine, allOf and anyOf, exception handling, timeouts, async variants, custom executors, and a parallel API-aggregation example.

9 min · 7 Q&A · 11 examples
CHAPTER 13

Synchronizers — CountDownLatch, CyclicBarrier, Semaphore & Phaser

Coordinating threads with CountDownLatch, CyclicBarrier, Semaphore, Phaser and Exchanger — how each works, real use cases such as startup checks and rate limiting, and how to choose between them.

8 min · 6 Q&A · 8 examples
CHAPTER 14

Concurrent Collections

Synchronized wrappers vs concurrent collections, fail-fast vs fail-safe (weakly consistent and snapshot) iterators, ConcurrentHashMap atomic operations, CopyOnWriteArrayList, the BlockingQueue family and ConcurrentLinkedQueue.

9 min · 7 Q&A · 8 examples
CHAPTER 15

Immutability, Thread Confinement & ThreadLocal

Why immutable objects are thread-safe, how Java supports immutability, thread confinement, ThreadLocal and its memory-leak trap in thread pools, InheritableThreadLocal, and Scoped Values.

9 min · 7 Q&A · 8 examples
CHAPTER 16

Fork/Join Framework & Parallel Streams

Divide-and-conquer with ForkJoinPool, RecursiveTask and RecursiveAction, work stealing, the common pool, and when parallel streams help — or hurt.

7 min · 6 Q&A · 6 examples
CHAPTER 17

Virtual Threads (Java 21)

Platform vs virtual threads, carrier threads and mounting, creating virtual threads, the thread-per-request model, pinning, why you don't pool virtual threads, Spring Boot support, and structured concurrency.

8 min · 7 Q&A · 4 examples
CHAPTER 18

Concurrency in Spring — @Async, synchronized & @Scheduled

synchronized vs @Async, enabling and configuring @Async, return types, the self-invocation trap, exception handling, context and transaction propagation, @Scheduled, and thread safety of Spring beans.

10 min · 7 Q&A · 12 examples
CHAPTER 19

Concurrency Coding Scenarios

Classic multithreading programs asked in interviews — odd/even printing, three threads in sequence, producer–consumer, a thread-safe singleton and cache, a bank transfer without deadlock, parallel aggregation, a rate limiter, and a quick revision sheet.

11 min · 10 Q&A · 10 examples