JUC Concurrent Programming
This article introduces the core concepts of Java multithreading and concurrent programming. It first explains the basic concepts of processes as units of resource allocation and threads as units of execution, as well as the difference between concurrency and parallelism. It then details three ways to create threads in Java (Thread, Runnable, and FutureTask) and thread state management. It focuses on how the synchronized keyword works, including the Monitor mechanism, lock escalation (unlocked → biased → lightweight → heavyweight), and optimization strategies such as lock spinning and lock elimination. It explains how the volatile keyword uses memory barriers to ensure visibility and ordering, and covers the wait-notify mechanism. It also describes the CAS approach to lock-free programming and compares optimistic and pessimistic locks. Finally, it introduces the atomic classes in the Java.util.concurrent.atomic package, including basic atomic classes, atomic references, atomic arrays, atomic field updaters, and atomic accumulators, showing how CAS enables efficient thread-safe operations.