Java Reference
In-Depth Information
the most efficient way to find it is to head over to Thread X 's house, hang out there until X is
finished, and then ask “Hey, what did you get for f (27)?”
Figure 5.3. Two Threads Computing the Same Value When Using Memoizer2 .
Listing 5.17. Replacing HashMap with ConcurrentHashMap .
We've already seen a class that does almost exactly this: FutureTask . FutureTask
represents a computational process that may or may not already have completed. Fu-
tureTask.get returns the result of the computation immediately if it is available; other-
wise it blocks until the result has been computed and then returns it.
Memoizer3 in Listing 5.18 redefines the backing Map for the value cache as a Concur-
rentHashMap<A, Future<V>> instead of a ConcurrentHashMap<A, V> . Memo-
izer3 first checks to see if the appropriate calculation has been started (as opposed to fin-
Search WWH ::




Custom Search