V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
amiwrong123
V2EX  ›  Java

感觉 Lambda 加上递归调用,就有点搞不懂运行过程了

  •  
  •   amiwrong123 · 2020-07-19 13:05:13 +08:00 · 2198 次点击
    这是一个创建于 1403 天前的主题,其中的信息可能已经有所发展或是发生改变。

    https://bugs.openjdk.java.net/browse/JDK-8240162

    这个 ConcurrentHashMap 的 bug,运行它的用例,会产生一个 bug,就是明明 map 中有映射,但 size 为 0 。

    import java.util.concurrent.*;
    
    public class test5 {
        public static void main(String[] args) {
    
            final ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
    
            final String key = "key";
            final String value = "value";
    
            map.put("anotherKey", "anotherValue");
            map.put(key, value);
    
            System.out.println("size=" + map.size());
    
            map.computeIfPresent(key, (aKey, aValue) -> {
                map.computeIfPresent(key, (bKey, bValue) -> null);
                return null;
            });
    
            System.out.println("size=" + map.size());//这句返回 0
            map.put(key, value);//这句打断点
            System.out.println("size=" + map.size());
        }
    }
    
    bug

    可以发现明明 map 中有映射,但 size 为 0 。

    1. 为什么没有产生无限递归,看起来会无限递归啊?感觉调用过程好难理解
    2. 为啥执行完,size 返回错误的值?

    运行环境 jdk8

    5 条回复    2020-07-20 19:33:00 +08:00
    amiwrong123
        1
    amiwrong123  
    OP
       2020-07-19 14:07:40 +08:00 via Android
    好吧,看错了,不会产生无限递归,不过咋一看这个过程确实有点搞不清楚
    312ybj
        2
    312ybj  
       2020-07-19 15:30:34 +08:00 via Android
    你可以尝试改变 debug 的类型,右击一下短点
    yanshenxian
        3
    yanshenxian  
       2020-07-19 17:09:32 +08:00   ❤️ 1
    ConcurrentHashMap 的 size 方法本来就是不精确的
    这个例子也没有递归操作啊 computeIfPresent 里的 lambda 返回空相当于 remove 这个 key 了,内部操作 count -1
    这就相当于操作了两次 -1 最后计算的 size 就是 0

    如果换成 HashMap 这里就会抛出 ConcurrentModificationException
    no1xsyzy
        4
    no1xsyzy  
       2020-07-20 13:24:38 +08:00
    没有哪个函数调用了自身,只是存在某处回调中再次调用同样的外部函数,你可能把它误看作是的递归了。
    amiwrong123
        5
    amiwrong123  
    OP
       2020-07-20 19:33:00 +08:00 via Android
    @no1xsyzy
    是的,并不是递归
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5121 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 02:42 · PVG 10:42 · LAX 19:42 · JFK 22:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.