Java Reference
In-Depth Information
lowed to proceed, and if not, the acquire blocks or fails. This decision is determined by the
synchronizer semantics; for example, acquiring a lock can succeed if the lock is unheld, and
acquiring a latch can succeed if the latch is in its terminal state.
The second part involves possibly updating the synchronizer state; one thread acquiring the
synchronizer can affect whether other threads can acquire it. For example, acquiring a lock
changes the lock state from “unheld” to “held”, and acquiring a permit from a
Semaphore
reduces the number of permits left. On the other hand, the acquisition of a latch by one thread
does not affect whether other threads can acquire it, so acquiring a latch does not change its
state.
Listing 14.13. Canonical Forms for Acquisition and Release in AQS.
A synchronizer supporting exclusive acquisition should implement the protected methods
tryAcquire
,
tryRelease
, and
isHeldExclusively
, and those supporting shared
acquisition should implement
tryAcquireShared
and
tryReleaseShared
. The
ac-
quire
,
acquireShared
,
release
, and
releaseShared
methods in AQS call the
try
forms of these methods in the synchronizer subclass to determine if the operation