请教一个 Java 中 CompletableFuture 的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
dumbbell5kg
V2EX    程序员

请教一个 Java 中 CompletableFuture 的问题

  •  
  •   dumbbell5kg 2023-09-22 12:02:04 +08:00 1880 次点击
    这是一个创建于 802 天前的主题,其中的信息可能已经有所发展或是发生改变。

    今天在看 Redisson 代码的时候,在 RedisExecutor<V, R>的 168 行发现这样一段

     connectionFuture.whenComplete((connection, e) -> { if (connectionFuture.isCancelled()) { connectionManager.getServiceManager().getShutdownLatch().release(); return; } if (connectionFuture.isDone() && connectionFuture.isCompletedExceptionally()) { return; } ..... }) 

    我的疑问是,已经在 whenComplete 中了,isDone()不是恒为 true 的吗,为什么这里要加这个判断?

    10 条回复    2023-09-22 15:13:02 +08:00
    keepfun
        1
    keepfun  
       2023-09-22 12:28:49 +08:00 via iPhone
    蹲个后续
    gosidealone
        2
    gosidealone  
       2023-09-22 13:01:22 +08:00
    没有吧 isDone 不包括抛出异常的情况吧 所以才要有后面那个判断?
    asssfsdfw
        3
    asssfsdfw  
       2023-09-22 13:28:22 +08:00
    脱了裤子放屁(没有喷的意思


    ```
    public boolean isDone() {
    return result != null;
    }
    ```

    ```
    public boolean isCompletedExceptionally() {
    Object r;
    return ((r = result) instanceof AltResult) && r != NIL;
    }
    ```
    nothingistrue
        4
    nothingistrue  
       2023-09-22 13:52:06 +08:00
    类上的说明,有这么一句:「 When two or more threads attempt to complete, completeExceptionally, or cancel a CompletableFuture, only one of them succeeds. 」。 所以 complete 跟 cancel 是互斥的。那么在 whenComplete 里面还去考虑 isCancelled 分支,确实是多余。

    此外,isDone 也说明了:「 Returns true if completed in any fashion: normally, exceptionally, or via cancellation 」。所以 isDone 包含了 isCompletedExceptionally 。后面那个分支里面,isDone 的判断也多余。
    nothingistrue
        5
    nothingistrue  
       2023-09-22 13:57:57 +08:00
    这是过渡防御的代码,功能测试,甚至 100%覆盖率可能都测不出来,只能靠代码评审去发现。而且有可能为了将来考虑,就算发现了也不一定会去改。
    dumbbell5kg
        6
    dumbbell5kg  
    OP
       2023-09-22 15:03:37 +08:00
    @nothingistrue isDone()也说了 cancel 是 complete 的一种,也就是可能是 cancel 导致走到了 whenComplete 中,为什么说 complete 跟 cancel 是互斥的?

    正因为 isDone 包含了 isCompletedExceptionally ,才在 isDone 后面判断了是 isDone 的哪种情况(isCompletedExceptionally ),这里我觉得也不多余。


    我的疑问点是 whenComplete 里面的 isDone 恒为 true ,那么在 whenComplete 里判断 isDone 不是多余了吗
    dumbbell5kg
        7
    dumbbell5kg  
    OP
       2023-09-22 15:06:16 +08:00
    @asssfsdfw isDone 和 isCompletedExceptionally 一起用没什么问题的,isDOne=true ,isCompletedExceptionally 也可能=false
    dumbbell5kg
        8
    dumbbell5kg  
    OP
       2023-09-22 15:08:33 +08:00
    @nothingistrue
    这里我撤回,是我看错了你的回复
    `正因为 isDone 包含了 isCompletedExceptionally ,才在 isDone 后面判断了是 isDone 的哪种情况(isCompletedExceptionally ),这里我觉得也不多余。`
    asssfsdfw
        9
    asssfsdfw  
       2023-09-22 15:11:04 +08:00
    @dumbbell5kg 既然 isCompletedExceptionally()都为 false 了,为什么还要 isDone ?
    dumbbell5kg
        10
    dumbbell5kg  
    OP
       2023-09-22 15:13:02 +08:00
    @asssfsdfw 我应该理解错了你的意思,忽略我的第一条回复,isCompletedExceptionally 前的 isDone 确实多余了
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5541 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 34ms UTC 06:09 PVG 14:09 LAX 22:09 JFK 01:09
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86