!38 Optimize ConcurrentHashMap#computeIfAbsent have performance problem in jdk1.8

Merge pull request !38 from kl/master
master
Java3y 2 years ago committed by Gitee
commit 0500a2016e
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

@ -24,9 +24,9 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
*
@ -45,8 +45,8 @@ public class AccountUtils {
/**
* /
*/
private Map<ChannelAccount, WxMpService> officialAccountServiceMap = new ConcurrentHashMap<>();
private Map<ChannelAccount, WxMaService> miniProgramServiceMap = new ConcurrentHashMap<>();
private ConcurrentMap<ChannelAccount, WxMpService> officialAccountServiceMap = new ConcurrentHashMap<>();
private ConcurrentMap<ChannelAccount, WxMaService> miniProgramServiceMap = new ConcurrentHashMap<>();
@Bean
public RedisTemplateWxRedisOps redisTemplateWxRedisOps() {
@ -63,15 +63,16 @@ public class AccountUtils {
* @param <T>
* @return
*/
@SuppressWarnings("unchecked")
public <T> T getAccountById(Integer sendAccountId, Class<T> clazz) {
try {
Optional<ChannelAccount> optionalChannelAccount = channelAccountDao.findById(Long.valueOf(sendAccountId));
if (optionalChannelAccount.isPresent()) {
ChannelAccount channelAccount = optionalChannelAccount.get();
if (clazz.equals(WxMaService.class)) {
return (T) miniProgramServiceMap.computeIfAbsent(channelAccount, account -> initMiniProgramService(JSON.parseObject(account.getAccountConfig(), WeChatMiniProgramAccount.class)));
return (T)ConcurrentHashMapUtils.computeIfAbsent(miniProgramServiceMap, channelAccount, account -> initMiniProgramService(JSON.parseObject(account.getAccountConfig(), WeChatMiniProgramAccount.class)));
} else if (clazz.equals(WxMpService.class)) {
return (T) officialAccountServiceMap.computeIfAbsent(channelAccount, account -> initOfficialAccountService(JSON.parseObject(account.getAccountConfig(), WeChatOfficialAccount.class)));
return (T)ConcurrentHashMapUtils.computeIfAbsent(officialAccountServiceMap, channelAccount, account -> initOfficialAccountService(JSON.parseObject(account.getAccountConfig(), WeChatOfficialAccount.class)));
} else {
return JSON.parseObject(channelAccount.getAccountConfig(), clazz);
}

@ -0,0 +1,41 @@
package com.java3y.austin.support.utils;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
/**
* @author kl
* @version 1.0.0
* @description ConcurrentHashMap util
* @date 2023/2/6 10:01
*/
public class ConcurrentHashMapUtils {
private static boolean IS_JAVA8;
static {
try {
IS_JAVA8 = System.getProperty("java.version").startsWith("1.8.");
} catch (Exception ignore) {
// exception is ignored
IS_JAVA8 = true;
}
}
/**
* Java 8 ConcurrentHashMap#computeIfAbsent
* @see <a href="https://bugs.openjdk.java.net/browse/JDK-8161372">https://bugs.openjdk.java.net/browse/JDK-8161372</a>
*
*/
public static <K, V> V computeIfAbsent(ConcurrentMap<K, V> map, K key, Function<? super K, ? extends V> func) {
if (IS_JAVA8) {
V v = map.get(key);
if (null == v) {
v = map.computeIfAbsent(key, func);
}
return v;
} else {
return map.computeIfAbsent(key, func);
}
}
}
Loading…
Cancel
Save