Can the following piece of code be rewritten w/o using Collections.synchronizedMap()
yet maintaining correctness at concurrency?
Collections.synchronizedMap(new WeakHashMap<Class, Object>());
i.e. is there something from java.util.concurrent one can use instead? Note that merely replacing with
new ConcurrentHashMap<Class, Object>(new WeakHashMap<Class, Object>()));
obviously won't work
ConcurrentHashMap
is that it can (through a variety of techniques) remain thread-safe under heavy load without (much) blocking. It is important to realize that if your class is not under heavy load, your performance with ConcurrentHashMap could be worse than with HashMap. If your environment is expected to be largely free of contention that you can use external synchronization and you'll be just fine. – Wnw