V (K key, V value) { do { Map<K, V> prevMap = myReference.get(); V existingValue = prevMap.get(key); if (existingValue != null) { return existingValue; } Map<K, V> newMap = new HashMap<>(prevMap); newMap.put(key, value); if (myReference.compareAndSet(prevMap, Collections.unmodifiableMap(newMap))) { return value; } } while(true); }