Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

> 尝试升级到5.8.23,使用了computeIfAbsent方法应该没有重复创建问题。 #3439

Closed
lichriszhang opened this issue Dec 20, 2023 · 2 comments
Labels

Comments

@lichriszhang
Copy link

          > 尝试升级到5.8.23,使用了computeIfAbsent方法应该没有重复创建问题。

我用的是jdk1.8,调用的是computeIfAbsentForJdk8,多线程情况下,因为没有加锁,map.get(key)会有多个线程返回null,mappingFunction.apply(key)还是存在多次执行的情况,所以问题仍然存在。

Originally posted by @lichriszhang in #3435 (comment)

@lichriszhang
Copy link
Author

lichriszhang commented Dec 20, 2023

针对jdk8的处理,虽然方法名是computeIfAbsent,但内部调用还是putIfAbsent

@looly
Copy link
Member

looly commented Dec 20, 2023

@lichriszhang sorry,我这边测试没有切换JDK导致的。

为了解决恶心的JDK-8161372死循环问题,不得不改造这个方法。

确实改造后由于非原子可能造成对象重复创建的问题。

不过我觉得这个重复创建并不影响最终单例对象的唯一性。

可以看下MapUtil.computeIfAbsentForJdk8方法:

image

步骤1中创建了对象,步骤2会重复判断,判断有值后会抛弃创建的对象。

也就是说,为了解决这个bug并照顾到性能问题,退而求其次选择了这个方案。

JDK9+无此问题。

@looly looly closed this as completed Dec 20, 2023
@looly looly added the question label Dec 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants