Concurrency
Раздел про многопоточность в Java простыми словами: потоки и модель памяти (JMM, happens-before, volatile), гонки, synchronized и блокировки, atomics и CAS, потокобезопасные коллекции, пулы потоков, CompletableFuture, виртуальные потоки Java 21 и типичные баги.
A section starting from scratch on multithreading and concurrency in Java: how threads and the memory model work, why races happen and how to guard against them, what tools are available — from synchronized to Java 21 virtual threads — and which bugs lie in wait. Best read in order.
- Threads and processes in Java — What a thread and a process are, why you need concurrency, how to start a thread in Java, and how a thread differs from a task — explained from scratch.
- The Java Memory Model (JMM): visibility and happens-before — Why two threads see different values for the same variable and how the Java Memory Model guarantees visibility: happens-before, volatile, and final fields in plain language.
- Races in concurrent programs: race condition and data race — We look at two kinds of races — race condition and data race — using a counter example, work out why ++ isn't atomic, and which three properties you need to control.
- synchronized and monitors: mutual exclusion in Java — In plain language: what a critical section and a monitor are, how synchronized protects shared data, which object to synchronize on, and how happens-before works.
- Explicit locks: Lock and ReentrantLock — When synchronized isn't enough: Lock, ReentrantLock, tryLock, fairness, ReadWriteLock — how explicit locks work in Java and when to use them.
- Atomic variables and CAS in Java — How AtomicInteger, AtomicReference, and CAS let you change data without locks, and when LongAdder does a better job than AtomicLong.
- Thread-safe collections in Java — Why HashMap and ArrayList break in concurrent code and how ConcurrentHashMap, CopyOnWriteArrayList, and BlockingQueue solve the problem.
- Thread pools: ExecutorService — Why you need a thread pool, how ExecutorService and ThreadPoolExecutor work, and how to shut a pool down properly — explained with Java 21 examples.
- CompletableFuture: asynchronous tasks in Java — A look at CompletableFuture: how to run tasks asynchronously, build chains, combine results, and handle errors gracefully.
- Virtual threads in Java 21 — What virtual threads are, how they differ from platform threads, and how the "thread per task" model becomes cheap again in Java 21.
- Structured concurrency: StructuredTaskScope — Fan-out without leaks: the scope as a unit of work, Joiner policies, cancelling the losers, and deadlines on the Java 25 API.
- Deadlock, livelock, and starvation: when threads get in each other's way — A look at three classic failures of concurrent code — deadlock, livelock, and starvation — what they are, why they happen, and how to guard against them.
- Concurrency in practice: Spring — Where all of this bites in a real service: singleton beans and races, a dedicated pool for @Async, pool exhaustion on blocking calls, @Transactional and threads, virtual threads in Spring Boot.