[concurrency-interest] CHM replaceAll

Ben Manes ben_manes at yahoo.com
Mon Feb 22 12:29:25 EST 2016

ConcurrentHashMap#replaceAll(bifunction) performs the computation optimistically per key, retrying until successful. This is a valid implementation, but slightly surprising due to the map's ability to perform the per-key operations pessimistically. In the case of a map holding a resource that should be explicitly closed, these races could result in a leak. That of course is a user error, but one that I think is easily corrected without a negative loss for the hash table. This of course is more important for my case of a cache, where I provide a custom implementation for a variety of reasons.
The request is to use computeIfPresent per key. The function would have to be decorated with a lambda that disallows null values. I suspect this single allocation is preferable to the cost of retrying on a conflict. That allocation could be removed with a more complex replacement, but that is probably unnecessary.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20160222/cfb0759a/attachment.html>

More information about the Concurrency-interest mailing list