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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to