死循环问题的提出:https://bugs.openjdk.java.net/browse/JDK-8062841 

 

        map.computeIfAbsent("AaAa",key->map.computeIfAbsent("BBBB",key2->42));

JDK1.8中ConcurrentHashMap中computeIfAbsent死循环bug

 

 

 

computeIfAbsent在1.8中才有的方法

JDK1.8中ConcurrentHashMap中computeIfAbsent死循环bug

 

 

computeIfAbsent意思是:key不存在时候,调用mappingFunction函数结果作为value值

debug

JDK1.8中ConcurrentHashMap中computeIfAbsent死循环bugJDK1.8中ConcurrentHashMap中computeIfAbsent死循环bug

 

 两个key的hash值一样,跑到同一个槽里面,然后一直在for循环判断各个if都不符合条件

 

 

 JDK1.8中ConcurrentHashMap中computeIfAbsent死循环bug

 

computeIfAbsent方法会初始化一个ReservationNode来占位,它会等待计算完毕后替换当前的占位对象。
这时候ConcurrentHashMap达到容量扩容而忽略了ReservationNode情况,调用put的时候在synchronized(f)没有对ReservationNode处理,所以会出现死循环。

 

 

在jdk1.8和1.9中对比

http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/concurrent/ConcurrentHashMap.java?r1=1.258&r2=1.259&sortby=date&diff_format=f

JDK1.8中ConcurrentHashMap中computeIfAbsent死循环bug

 

相关文章:

  • 2021-09-14
  • 2021-11-18
  • 2023-03-09
  • 2022-01-08
  • 2021-12-12
  • 2021-07-15
  • 2021-05-27
  • 2022-12-23
猜你喜欢
  • 2021-09-25
  • 2021-08-18
  • 2021-05-16
  • 2021-10-25
  • 2022-01-05
  • 2021-10-02
相关资源
相似解决方案