NOTE
Building an AQS-Style Synchronizer
A teaching implementation of a synchronizer using atomic state, a FIFO wait queue, park/unpark, and release wakeups, emphasizing invariants rather than copying JDK internals.
This is a historical learning note and may contain outdated or incomplete understanding.
1. Learning Goal
A small AQS-like implementation helps explain why a blocking lock needs more than a boolean.
Core pieces are:
- atomic ownership/state;
- a concurrent wait queue;
- a protocol for enqueue/cancel;
parkto suspend losers;unparkto wake a successor after release.
2. Fast Path
A thread first attempts an atomic state transition. If uncontended, no queue or park is required.
3. Slow Path
On failure:
- create/enqueue a waiter node safely;
- wait until it is eligible to retry;
- park to avoid wasting CPU;
- handle interrupts/timeouts/cancellation;
- retry state acquisition after wakeup.
4. Release
The owner changes state atomically and wakes an appropriate successor when the synchronizer becomes available.
5. Hard Parts
A production-quality synchronizer must handle races among:
- enqueue and release;
- cancellation and successor discovery;
- interrupt/timeout;
- reentrancy;
- fairness;
- memory ordering.
That is why application code should normally use JUC synchronizers rather than implementing a queueing lock from scratch.
6. Do Not Copy Old AQS Source Blindly
This historical note’s source walk-through is useful conceptually, but JDK AQS internals evolve. Treat exact Node layouts/status constants/Unsafe calls as version-specific.