I want a collection in Java which:
- maps arbitrary
Object
s toObject
s (notString
or 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.