Locks, conditions and AQS question 35 of 180 senior
What is AbstractQueuedSynchronizer and which classes are built on it?
- A base class for thread pools
- Correct: A framework holding an atomic int state plus a CLH wait queue, used by ReentrantLock, Semaphore, CountDownLatch and ReentrantReadWriteLock
- The implementation of
synchronized - A lock-free queue implementation
AQS gives you one volatile int state, CAS operations on it, and a FIFO queue of parked threads. A
synchroniser subclasses it and defines what the state means and when acquisition succeeds. Everything
else, the queueing, parking, unparking and cancellation, is inherited.
ReentrantLock uses state as a hold count. Semaphore uses it as a permit count. CountDownLatch
uses it as the remaining count and never lets it go back up. ReentrantReadWriteLock packs readers
and writers into two halves of the same int. ThreadPoolExecutor.Worker uses it too, as a small non-reentrant lock. FutureTask did until Java 7,
and has used its own state field and stack of waiters since.
Note that synchronized is not on the list: monitors are implemented in the JVM, not in Java, which is
why a thread waiting on a ReentrantLock appears as WAITING at LockSupport.park rather than as
BLOCKED.
Quiz yourself on Locks, conditions and AQS Suggest a correction