Use of a key to synchronize access to code block
问题 Normally I would lock on a critical section like the following. public class Cache { private Object lockObject = new Object(); public Object getFromCache(String key) { synchronized(lockObject) { if (cache.containsKey(key)) { // key found in cache - return cache value } else { // retrieve cache value from source, cache it and return } } } } The idea being I avoid a race condition which could result in the data source being hit multiple times and the key being added to the cache multiple times.