Java Reference
In-Depth Information
Listing 14.14. Binary Latch Using
AbstractQueuedSynchronizer
.
In
OneShotLatch
, the AQS state holds the latch state—closed (zero) or open (one). The
await
method calls
acquireSharedInterruptibly
in AQS, which in turn consults
the
tryAcquireShared
method in
OneShotLatch
. The
tryAcquire-Shared
im-
plementation must return a value indicating whether or not acquisition can proceed. If the
latch has been previously opened,
tryAcquireShared
returns success, allowing the
thread to pass; otherwise it returns a value indicating that the acquisition attempt failed.
The
acquireSharedInterruptibly
method interprets failure to mean that the thread
should be placed on the queue of waiting threads. Similarly,
signal
calls
re-
leaseShared
, which causes
tryReleaseShared
to be consulted. The
tryReleaseShared
implementation unconditionally sets the latch state to open and in-
dicates (through its return value) that the synchronizer is in a fully released state. This causes
AQS to let all waiting threads attempt to reacquire the synchronizer, and acquisition will now
succeed because
tryAcquireShared
returns success.