I want a collection in Java which:
- maps arbitrary
Objects toObjects (notStringor otherwise restricted keys only) - will be used as a cache; if the key is not in the cache, a value will be computed (this doesn’t have to be built into the collection)
- will be accessed from multiple threads simultaneously
- will never have items removed from it
- must be very efficient to read (cache hit); not necessarily efficient to write (cache miss)
It’s OK if cache misses simultaneously in multiple threads cause redundant computations; the typical case is that the cache is mostly filled by one thread at first.
A synchronized block around a thread-unsafe hashtable fails the efficient-to-read criterion. Thread-local caches would be straightforward but mean that new threads are expensive since they have full copies of the cache.
Java 1.5 built-ins, or one-or-few class files we can copy into our MIT-licensed project, are preferred, rather than large external libraries.
Use the java concurrent hashmap
N.b. This is not longer a general solution for multithreaded caching, since it relies on the stated fact that objects are immutable, and thus object equivalence is not important.