Java Reference
In-Depth Information
by
happens-before
. This could be done by making the reference to the result volatile, but it is
possible to exploit existing synchronization to achieve the same result at lower cost.
FutureTask is carefully crafted to ensure that a successful call to
tryReleaseShared
al-
ways
happens-before
a subsequent call to
tryAcquireShared
;
tryReleaseShared
always writes to a volatile variable that is read by tryAcquireShared. Listing 16.2 shows the
innerSet and
innerGet
methods that are called when the result is saved or retrieved; since
innerSet writes result before calling releaseShared (which calls tryReleaseShared) and
in-
nerGet
reads result after calling acquireShared (which calls tryAcquireShared), the pro-
gram order rule combines with the volatile variable rule to ensure that the write of result in
innerGet
happens-before
the read of result in
innerGet
.