[concurrency-interest] ConcurrentHashMapV8 insertion/update methods

Aleksey Shipilev aleksey.shipilev at oracle.com
Tue Aug 7 06:07:20 EDT 2012


Hi Doug,

On 08/07/2012 04:25 AM, Doug Lea wrote:
> This hits all possible cases of atomically skipping,
> writing a value, or apply a function when the key
> is absent or present. In principle, all of these things
> can be done solely with the "compute" method, but it would
> often require the use of heavy functions that could tie up
> mappings and lead to more contention, which we'd like to help
> people avoid.

Any specific example? Assuming returning null to compute means "no
update", users will have to translate as follows:

 computeIfAbsent(k, f1) =
    compute(k, (k, curV) -> { (curV != null) ? null : f1(k); });

 computeIfPresent(k, f2) =
    compute(k, (k, curV) -> { (curV == null) ? null : f2(k, curV) });

 merge(k, v, f3) =
    compute(k, (k, curV, final v) ->
                  { (curV == null) ? v : f3(curV, final v) });

 (keyed) merge(k, v, f4) =
    compute(k, (k, curV, final v) ->
                  { (curV == null) ? v : f4(k, curV, final v) });

It is a straight-forward translation, where is the heavy functions and
contention is kicking back?

-Aleksey.


More information about the Concurrency-interest mailing list