Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Beware of Slightly Different Behavior Between ConcurrentMap and ConcurrentHashMap for computeIfAbsent

DZone's Guide to

Beware of Slightly Different Behavior Between ConcurrentMap and ConcurrentHashMap for computeIfAbsent

The key difference is in how many times the implementation can be called.

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

So I was refactoring some code to use non-locking collection classes and I noticed this significant difference between ConcurrentMap.computeIfAbsent and ConcurrentHashMap.computeIfAbsent. The key difference is that for the former, default implementing the mapping function can be called many times for a particular key, whereas for the concrete implementation it will be called only once.

Right, okay, so armed with that you know that any collection that implements just ConcurrentMap will inherit this behavior; further to that I found in particular that Guava would return a different implementation depending on the passed on parameters:

ConcurrentMap map1 = new MapMaker().makeMap();
System.out.println(map1.getClass());

ConcurrentMap map2 = new MapMaker().weakKeys().makeMap();
System.out.println(map2.getClass());

....

class java.util.concurrent.ConcurrentHashMap
class com.google.common.collect.MapMakerInternalMap

MapMakerInternalMap doesn't override computerIfAbsent, therefore the behaviour of this function will be significantly different depending on the parameters to pass into the maker, something that might not be apparent from the get go.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ,concurrency

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}