JUC
Java · 45 notes
- 6.1 Start with the Hardwarehistorical
Understanding the Java Memory Model starts with CPU, memory, and cache relationships. These notes introduce the hardware issues through the von Neumann architecture.
- 6.2 2.Synchronizedhistorical
Synchronized protects Java synchronization blocks. These notes examine usage and implementation through atomicity, ordering, visibility, and locking overhead.
- 6.3 3.volatilehistorical
These volatile notes examine ordering, visibility, and atomicity, compare volatile with synchronized, and record usage scenarios.
- 6.4 CAShistorical
CAS can be introduced through the distinction between optimistic and pessimistic locking. These notes compare conflict handling before examining compare-and-swap.
- 6.5 5.AQShistorical
AQS is a queued synchronizer for building JUC concurrency utilities. These notes examine when and how to use it for custom synchronization tools.
- 6.6 6.ReentrantLockhistorical
ReentrantLock provides explicit locking and unlocking. These notes compare it with synchronized and trace its implementation through AQS-related classes.
- 6.7 7.Lock Conditionhistorical
Condition works with Lock for condition waiting and notification. These notes compare it with Object.wait and notify through producer-consumer scenarios.
- 6.8 8.CyclicBarrierhistorical
CyclicBarrier makes threads wait for one another before proceeding and can be reused. These notes trace its implementation through Lock and Condition.
- 6.9 9.CountDownLatchhistorical
CountDownLatch lets threads wait for tasks to complete and resumes them when the counter reaches zero. These notes trace its one-shot behavior through AQS.
- 6.10 10.CopyOnWriteArrayListhistorical
CopyOnWriteArrayList separates reads from writes, allowing unlocked reads while copying data on writes. These notes examine the mechanism and its consistency behavior.
- 6.11 11.CopyOnWriteArraySethistorical
CopyOnWriteArraySet stores distinct elements using CopyOnWriteArrayList. These notes trace its copy-on-write implementation through methods such as addIfAbsent.
- 6.12 ArrayBlockingQueuehistorical
ArrayBlockingQueue is a bounded blocking queue backed by an object array. These notes trace waiting and notification through its shared lock.
- 6.13 13.ThreadLocalhistorical
ThreadLocal stores independent data for each thread rather than sharing one variable's state. These notes examine its internal map through data isolation.
- 6.14 14.ThreadPoolhistorical
Thread pools separate submission from execution and reuse threads to reduce creation and destruction. These notes examine Java pool structure and execution.
- 6.15 CompletableFuturehistorical
CompletableFuture organizes asynchronous computation by running work on separate threads and continuing processing when results become available.
- 6.16 ConcurrentHashMap in JDK 1.7historical
JDK 1.7 ConcurrentHashMap uses segmented locking with arrays and linked lists for concurrent updates. These notes trace the implementation through Segment and HashEntry.
- 6.17 fork_joinhistorical
Fork/Join splits large work into parallel subtasks and combines their results. These notes introduce the framework through limitations of ordinary thread pools.
- 6.18 Exchangerhistorical
Exchanger: Used to exchange data between two threads. The data flow is bidirectional. That is, if there are two threads, Thread1 and Thread2, Thread1 sends one piece of data to Thread2, while Thread2 also sends one piece of data to Thread1.
- 6.19 ReentrantReadWriteLockhistorical
Exclusive locks prevent concurrent readers, while read-write locks allow them. These notes introduce ReentrantReadWriteLock through that need.
- 6.20 Semaphorehistorical
Semaphore limits concurrent resource access through permits. These notes examine fair and nonfair policies and permit acquisition and release.
- 6.21 Kernel-Level Threadhistorical
User-level threads are managed by applications, while kernel threads are visible to and scheduled by the operating system. These notes connect the distinction with Java threads.
- 6.22 Unsafehistorical
Unsafe exposes low-level operations such as direct memory access. These notes introduce them through C pointers and memory allocation and examine why care is required.
- 6.23 Revisiting the JMMhistorical
Processor architectures impose different constraints on memory operations. These notes examine how JMM abstracts those differences through observable execution order.
- 6.24 Lock Optimizationhistorical
Java has introduced several synchronized optimizations. These notes begin with lock elimination and examine how compilers and runtimes reduce synchronization costs.
- 6.25 Implementing a Simple AQS by Handhistorical
Writing a simplified AQS illustrates how an exclusive lock records its owner and how other threads wait and resume.
- 6.27 BlockingQueuehistorical
BlockingQueue provides a thread-safe blocking-queue interface. These notes examine enqueue and dequeue operations, waiting, timeouts, and failure behaviors.
- 6.28 Executorshistorical
Executors provides factory methods for different thread pools. These notes compare cached, fixed-size, single-threaded, and scheduled pools through examples.
- 6.29 ConcurrentHashMap in JDK 1.8historical
JDK 1.8 ConcurrentHashMap combines synchronized, CAS, arrays, linked lists, and red-black trees for thread-safe access. These notes trace execution through methods such as putVal.
- 6.30 Producer-Consumerhistorical
Producers and consumers wait when conditions are unmet and wake one another when work can proceed. These notes compare wait/notify and two-Condition approaches.
- 6.31 Nonfair ReadWriteLockhistorical
These notes follow ReentrantReadWriteLock source code to examine nonfair read-write lock acquisition and direct acquisition attempts.
- 6.32 Nonfair Semaphorehistorical
A nonfair semaphore allows direct competition for permits. These notes trace acquisition and waiting through NonfairSync.
- 6.33 Thread.sleephistorical
After Thread.sleep ends, a thread can compete for the CPU again but is not guaranteed immediate execution. These notes illustrate that distinction with a timing example.
- 6.34 Nonfair Lockhistorical
A nonfair lock allows threads to attempt acquisition when the lock becomes free without requiring arrival order. These notes trace NonfairSync.lock.
- 6.35 LinkedBlockingQueuehistorical
LinkedBlockingQueue uses a singly linked list with separately coordinated reads and writes. These notes trace waiting and notification through its node structure.
- 6.36 RejectedExecutionHandlerhistorical
When a thread pool's workers and queue are full, a rejection handler processes new tasks. These notes examine RejectedExecutionHandler implementations.
- 6.37 Deadlockhistorical
Java examples create a deadlock and inspect thread states to show how mutual waiting prevents progress.
- 6.38 Fair ReadWriteLockhistorical
These notes follow ReentrantReadWriteLock source code to examine fair read-write locking and how the wait queue participates in acquisition.
- 6.39 Fair Semaphorehistorical
A fair semaphore allocates permits under a fairness policy. These notes trace permit acquisition and queuing through FairSync.
- 6.40 Thread Stateshistorical
Java threads transition between execution states. These notes use the thread-state enumeration and a state diagram to organize those relationships.
- 6.41 PriorityBlockingQueuehistorical
PriorityBlockingQueue uses a binary heap for a priority-ordered blocking queue. These notes examine its array representation, ordering, and waiting on an empty queue.
- 6.42 SynchronousQueuehistorical
SynchronousQueue stores no elements, so writes must pair with reads or wait. These notes trace the process through its transfer structures.
- 6.43 Atomichistorical
Atomic classes provide thread-safe atomic operations through mechanisms such as CAS. These notes trace implementation using AtomicInteger.
- Estimating Thread Pool Sizehistorical
Thread counts can be estimated from QPS and response time, then checked with load testing. These notes record that estimation and validation process.
- Multithreaded Folder Size Statisticshistorical
Directory-size calculation is split across threads and their results combined, illustrating how to organize parallel tasks.