You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by "leeyiyu (via GitHub)" <gi...@apache.org> on 2023/06/09 05:52:59 UTC

[GitHub] [rocketmq] leeyiyu opened a new issue, #6879: [Bug] ConcurrentHashMapUtils fails to solve the loop bug in JDK8.

leeyiyu opened a new issue, #6879:
URL: https://github.com/apache/rocketmq/issues/6879

   ### Before Creating the Bug Report
   
   - [X] I found a bug, not just asking a question, which should be created in [GitHub Discussions](https://github.com/apache/rocketmq/discussions).
   
   - [X] I have searched the [GitHub Issues](https://github.com/apache/rocketmq/issues) and [GitHub Discussions](https://github.com/apache/rocketmq/discussions)  of this repository and believe that this is not a duplicate.
   
   - [X] I have confirmed that this bug belongs to the current repository, not other repositories of RocketMQ.
   
   
   ### Runtime platform environment
   
   Mac  OS 13.0
   
   ### RocketMQ version
   
   develop
   
   ### JDK Version
   
   jdk8
   
   ### Describe the Bug
   
   [ConcurrentHashMapUtils](https://github.com/apache/rocketmq/blob/develop/common/src/main/java/org/apache/rocketmq/common/utils/ConcurrentHashMapUtils.java) 中提供的computeIfAbsent方法没有解决jdk8的循环问题,复现代码如下
   
   ```
   ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
   map.computeIfAbsent("AaAa", key->map.computeIfAbsent("BBBB",key2->"42"));
   ```
   
   解决方案
   ```
   public static <K, V> V computeIfAbsent(ConcurrentMap<K, V> map, K key, Function<? super K, ? extends V> func) {
           Objects.requireNonNull(func);
           if (isJdk8) {
               V v = map.get(key);
               if (null == v) {
   //                v = map.computeIfAbsent(key, func);
   
                   // this bug fix methods maybe cause `func.apply` multiple calls.
                   v = func.apply(key);
                   if (null == v) {
                       return null;
                   }
                   final V res = map.putIfAbsent(key, v);
                   if (null != res) {
                       // if pre value present, means other thread put value already, and putIfAbsent not effect
                       // return exist value
                       return res;
                   }
               }
               return v;
           } else {
               return map.computeIfAbsent(key, func);
           }
       }
   ```
   
   
   ### Steps to Reproduce
   
   ```
   ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
   map.computeIfAbsent("AaAa", key->map.computeIfAbsent("BBBB",key2->"42"));
   ```
   
   ### What Did You Expect to See?
   
   死循环
   
   ### What Did You See Instead?
   
   符合原先预期
   
   ### Additional Context
   
   _No response_


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@rocketmq.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [rocketmq] dingshuangxi888 commented on issue #6879: [Bug] ConcurrentHashMapUtils fails to solve the loop bug in JDK8.

Posted by "dingshuangxi888 (via GitHub)" <gi...@apache.org>.
dingshuangxi888 commented on issue #6879:
URL: https://github.com/apache/rocketmq/issues/6879#issuecomment-1635494730

   I understand that it's not necessary to use it that way, and there aren't any use cases for it currently.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [rocketmq] yuz10 closed issue #6879: [Bug] ConcurrentHashMapUtils fails to solve the loop bug in JDK8.

Posted by "yuz10 (via GitHub)" <gi...@apache.org>.
yuz10 closed issue #6879: [Bug] ConcurrentHashMapUtils fails to solve the loop bug in JDK8.
URL: https://github.com/apache/rocketmq/issues/6879


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org