[concurrency-interest] generalise computeIfAbsent() for ConcurrentMap

Mark Thornton mthornton at optrak.com
Wed Jun 20 10:56:24 EDT 2012


On 20/06/12 15:27, Aleksey Shipilev wrote:
> I am puzzled now. Why wouldn't usual stub suffice as the default implementation?
>
>      @Override
>      public V computeIfAbsent(K key,
> ConcurrentHashMapV8.MappingFunction<? super K, ? extends V>
> mappingFunction) {
>          V v = get(key);
>          if (v == null) {
>              V newV = mappingFunction.map(key);
>              V oldV = putIfAbsent(key, newV);
>              v = (oldV == null) ? newV : oldV;
>          }
>          return v;
>      }
>
> Is this an incorrect defender?

Yes, if the mappingFunction has side effects, as it may be called more 
than once for a key.

Mark



More information about the Concurrency-interest mailing list