"Explain how AQS (AbstractQueuedSynchronizer) works and how ReentrantLock uses it."
AQS is the foundation of java.util.concurrent locks. Senior Java interviews probe AQS to check whether you can read the JDK source and understand the primitives underlying every lock, semaphore, and latch in the standard library.
AQS core: an int state variable (volatile) plus a FIFO CLH-style queue of waiting threads. Subclasses interpret state — ReentrantLock uses it as hold count, Semaphore as permits, CountDownLatch as remaining count.
Acquire flow: tryAcquire (subclass defines, typically CAS on state); if it fails, enqueue a node and park. Release flow: tryRelease (subclass defines), if successful unpark the head's next node.
CLH queue: doubly-linked list of Nodes. Each node represents a waiting thread. Node has prev/next pointers and a waitStatus field. Head is a dummy sentinel; threads wait at non-head nodes.
ReentrantLock: exclusive mode. tryAcquire CAS state from 0 to 1 to grab the lock; same thread re-entering increments state. tryRelease decrements state; when it hits 0, release the lock and wake next waiter.
Other AQS-based classes: Semaphore uses shared mode (multiple acquirers can succeed), CountDownLatch is shared with state as count, ReentrantReadWriteLock packs read-count and write-count into the single state int.
AQS — AbstractQueuedSynchronizer — is the shared infrastructure underneath almost every lock class in java.util.concurrent. It provides two things: a volatile int state, and a FIFO CLH-style queue of waiting threads. Subclasses define what state means. ReentrantLock uses state as the hold count. Semaphore uses it as remaining permits. CountDownLatch uses it as the remaining countdown. The acquire flow: a thread calls tryAcquire (which subclasses implement, typically a CAS on state — for ReentrantLock, CAS state from 0 to 1). If tryAcquire succeeds, the thread owns the resource. If it fails, AQS enqueues a Node representing this thread into the CLH queue, then parks the thread via LockSupport.park(). The release flow: the holder calls tryRelease, which subclasses implement (for ReentrantLock, decrement state; when it hits 0, null out the exclusive owner); if tryRelease succeeds and the queue is non-empty, AQS unparks the head's next node, which then re-runs tryAcquire and, on success, takes over. CLH queue details: doubly linked list of Node objects. Each Node has prev, next, thread, and waitStatus. Head is a dummy sentinel; the first real waiter is head.next. Threads spin-then-park while waiting. On cancellation or interruption, the node is marked cancelled and eventually spliced out. ReentrantLock itself has two subclasses — FairSync and NonfairSync. Nonfair (default) tries to barge: a new thread can call tryAcquire and steal the lock even if the queue has waiting threads. Fair tryAcquire first checks hasQueuedPredecessors() and skips acquire if anyone is ahead. Nonfair has higher throughput; fair prevents starvation. ReentrantReadWriteLock is a clever trick — it packs write count in the low 16 bits of state and read count in the high 16 bits, so a single CAS can represent both. Understanding AQS lets you read the JDK source for any lock in java.util.concurrent, and also lets you build custom synchronizers by subclassing.
State meaning is subclass-specific — saying this shows real source-level understanding.
FairSync vs NonfairSync distinction is a classic follow-up — know barging.
Mentioning the state int packing trick in ReentrantReadWriteLock is a strong signal.
If you blank on CLH or tryAcquire/tryRelease during the interview, HireMe AI can surface these names.
CLH fits AQS's cancellation model better — nodes can be spliced out cleanly. The doubly-linked prev pointer is what makes cancellation splicing work.
Parking releases the CPU. Busy-waiting wastes cycles and causes cache coherence traffic. AQS does a brief spin before parking for the short-wait case.
Yes — subclass AbstractQueuedSynchronizer or AbstractQueuedLongSynchronizer and implement tryAcquire/tryRelease. This is how you build custom synchronizers.
HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.
Try HireMe AI Free