diff --git a/CHANGELOG.md b/CHANGELOG.md index 2176a3bf0..0404638e4 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ * 【db 】 修复count查询别名问题(issue#I590YB@Gitee) * 【json 】 修复json中byte[]无法转换问题(issue#I59LW4@Gitee) * 【core 】 修复NumberUtil.isXXX未判空问题(issue#2350@Github) +* 【core 】 修复Singleton中ConcurrentHashMap在JDK8下的bug引起的可能的死循环问题(issue#2349@Github) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/Singleton.java b/hutool-core/src/main/java/cn/hutool/core/lang/Singleton.java index 4a60648ad..ab26a662b 100755 --- a/hutool-core/src/main/java/cn/hutool/core/lang/Singleton.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/Singleton.java @@ -52,7 +52,15 @@ public final class Singleton { */ @SuppressWarnings("unchecked") public static T get(String key, Func0 supplier) { - return (T) POOL.computeIfAbsent(key, (k)-> supplier.callWithRuntimeException()); + //return (T) POOL.computeIfAbsent(key, (k)-> supplier.callWithRuntimeException()); + // issues#2349 + // ConcurrentHashMap.computeIfAbsent在某些情况下会导致死循环问题,此处采用Dubbo的解决方案 + Object value = POOL.get(key); + if(null == value){ + POOL.putIfAbsent(key, supplier.callWithRuntimeException()); + value = POOL.get(key); + } + return (T) value; } /**