Java Reference
In-Depth Information
can proceed. The synchronizer subclass can use getState , setState , and com-
pareAndSetState to examine and update the state according to its acquire and release
semantics, and informs the base class through the return status whether the attempt to acquire
or release the synchronizer was successful. For example, returning a negative value from
tryAcquireShared indicates acquisition failure; returning zero indicates the synchron-
izer was acquired exclusively; and returning a positive value indicates the synchronizer was
acquired nonexclusively. The tryRelease and tryReleaseShared methods should
return true if the release may have unblocked threads attempting to acquire the synchron-
izer.
To simplify implementation of locks that support condition queues (like ReentrantLock ),
AQS also provides machinery for constructing condition variables associated with synchron-
izers.
14.5.1. A Simple Latch
OneShotLatch in Listing 14.14 is a binary latch implemented using AQS. It has two pub-
lic methods, await and signal , that correspond to acquisition and release. Initially, the
latch is closed; any thread calling await blocks until the latch is opened. Once the latch is
opened by a call to signal , waiting threads are released and threads that subsequently ar-
rive at the latch will be allowed to proceed.
Search WWH ::




Custom Search