[concurrency-interest] ConcurrentHashMap computeIfAbsent

Viktor Klang viktor.klang at gmail.com
Sun Dec 21 13:07:55 EST 2014


On Sun, Dec 21, 2014 at 4:47 PM, Doug Lea <dl at cs.oswego.edu> wrote:

> On 12/21/2014 09:59 AM, Viktor Klang wrote:
>
>> For "computeIfAbsent"-style maps which are intended for multithreaded
>> use, I
>> tend to prefer to use ConcurrentMap[Key, CompletionStage[Value]],
>>
>
> Right. Prior to jdk8, we suggested variations of this and other
> techniques that remain preferable in many cases. However, the
> most common CHM user error was trying (but failing) to emulate
> what is now supported as computeIfAbsent. This is an improvement,
> but leads to new (much less common) kinds of potential errors.
> We cannot automatically eliminate or trap all of them. It
> might be possible to do more than we do now, either internally
> (inside CHM) or, more likely, with improved developer tools.


Absolutely, and the fact that there was only blocking Futures in the JDK at
that time.
Having "monadic" Futures like CompletableFuture means you can avoid
blocking completely (assuming that the CHM impl is non-blocking). This is
highly useful when using it as a cache, and especially if there's a very
skewed read distribution, paired with a multitude of readers.


>
>
> -Doug
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>



-- 
Cheers,
√
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20141221/4822eadc/attachment.html>


More information about the Concurrency-interest mailing list