Java Reference
In-Depth Information
Listing 5.18. Memoizing Wrapper Using FutureTask .
Memoizer3 is vulnerable to this problem because a compound action (put-if-absent) is per-
formed on the backing map that cannot be made atomic using locking. Memoizer in Listing
5.19 takes advantage of the atomic putIfAbsent method of ConcurrentMap , closing
the window of vulnerability in Memoizer3 .
Caching a Future instead of a value creates the possibility of cache pollution : if a compu-
tation is cancelled or fails, future attempts to compute the result will also indicate cancella-
tion or failure. To avoid this, Memoizer removes the Future from the cache if it detects
that the computation was cancelled; it might also be desirable to remove the Future upon
Search WWH ::




Custom Search