Java Reference
In-Depth Information
Listing 5.16. Initial Cache Attempt Using HashMap and Synchronization.
Memoizer1 in Listing 5.16 shows a first attempt: using a HashMap to store the results
of previous computations. The compute method first checks whether the desired result is
already cached, and returns the precomputed value if it is. Otherwise, the result is computed
and cached in the HashMap before returning.
HashMap is not thread-safe, so to ensure that two threads do not access the HashMap at
the same time, Memoizer1 takes the conservative approach of synchronizing the entire
Search WWH ::




Custom Search