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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.