[ 
https://issues.apache.org/jira/browse/GEODE-6410?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16777204#comment-16777204
 ] 

Bill Burcham commented on GEODE-6410:
-------------------------------------

Some observations on the differences between {{computeIfAbsent()}} and 
{{putIfAbsent()}}

Have a look at the two JDK tickets:

[https://bugs.openjdk.java.net/browse/JDK-8161372]
 [https://bugs.openjdk.java.net/browse/JDK-6737839]

The first one (for {{computeIfAbsent()}}) is fixed in JDK11. The second one 
(for {{putIfAbsent()}}) is not.

Initially I thought these two methods differed only in the way the value was 
computed (or not). But the differences go deeper:

[https://stackoverflow.com/questions/48183999/what-is-the-difference-between-putifabsent-and-computeifabsent-in-java-8-map/48184207#48184207]

Difference #2 in that citation is:

!image-2019-02-25-10-38-24-493.png!

While {{computeIfAbsent}} can be viewed as a {{get()}} with a fallback value 
(the result of applying the method reference),  {{putIfAbsent()}} does not work 
that way. {{putIfAbsent()}} is like a regular old {{get()}} that returns the 
existing (old) value (or null if there was none) and then sets a fallback value 
if there was no value before.

I suppose users might rightfully expect no write-locks in either case (if a 
value was present before the call). But the implementations of the two are 
pretty different.

This is what the naive fix to both methods would look like:

{code}
  private static class MyMap<K,V> extends ConcurrentHashMap<K,V> {

    @Override
    public V computeIfAbsent(final K key, final Function<? super K, ? extends 
V> mappingFunction) {
      final V val = get(key);
      if (null == val)
        return super.computeIfAbsent(key, mappingFunction);
      else
        return val;
    }

    @Override
    public V putIfAbsent(final K key, final V value) {
      final V val = get(key);
      if (null == val)
        super.putIfAbsent(key, value);
      return val;
    }

  }
{code}

> review use of putIfAbsent
> -------------------------
>
>                 Key: GEODE-6410
>                 URL: https://issues.apache.org/jira/browse/GEODE-6410
>             Project: Geode
>          Issue Type: Bug
>          Components: general
>            Reporter: Jason Huynh
>            Assignee: Bill Burcham
>            Priority: Major
>         Attachments: image-2019-02-25-10-38-24-493.png
>
>
> Usages of putIfAbsent in Geode on ConcurrentHashMap may not have realized the 
> actual synchronized/atomic nature of the putIfAbsent.  See 
> [https://bugs.openjdk.java.net/browse/JDK-6737839]
> This ticket is for someone to review or possibly change the putIfAbsent 
> usages to be more performant. 
> Not sure if putIfAbsent is called enough and under contention enough for this 
> to matter...



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to