Understanding Java AbstractQueuedSynchronizer
AbstractQueuedSynchronizer (AQS) is the framework behind many Java concurrency utilities, including locks, semaphores, latches, and read-write synchronization.
AQS stores synchronization state in a volatile integer and exposes atomic state operations. Subclasses define what the state means by implementing methods such as tryAcquire, tryRelease, tryAcquireShared, and tryReleaseShared.
When acquisition fails, AQS places the thread in a FIFO-style synchronization queue. Nodes track predecessor, successor, wait status, and whether acquisition is exclusive or shared. Threads normally park with LockSupport.park and are unparked when their predecessor releases or propagation is required.
Exclusive mode permits one successful owner at a time. Shared mode can allow several threads to proceed, as with Semaphore or CountDownLatch. Cancellation and interruption require careful queue repair so that successors are not stranded.
A ConditionObject maintains a separate condition queue. await releases the lock and moves the thread to the condition queue; signal transfers a waiter back to the synchronization queue, where it must reacquire the lock before returning.
Custom synchronizers should keep acquisition methods small, nonblocking, and free of side effects. Prefer established JDK primitives unless the synchronization semantics truly require a new AQS subclass.