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

请教一个 Java 中 CompletableFuture 的问题

  •  
  •   dumbbell5kg · 218 天前 · 1296 次点击
    这是一个创建于 218 天前的主题,其中的信息可能已经有所发展或是发生改变。

    今天在看 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
    fenglangjuxu
        1
    fenglangjuxu  
       218 天前 via iPhone
    蹲个后续
    gosidealone
        2
    gosidealone  
       218 天前
    没有吧 isDone 不包括抛出异常的情况吧 所以才要有后面那个判断?
    asssfsdfw
        3
    asssfsdfw  
       218 天前
    脱了裤子放屁(没有喷的意思


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

    ```
    public boolean isCompletedExceptionally() {
    Object r;
    return ((r = result) instanceof AltResult) && r != NIL;
    }
    ```
    nothingistrue
        4
    nothingistrue  
       218 天前
    类上的说明,有这么一句:「 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  
       217 天前
    这是过渡防御的代码,功能测试,甚至 100%覆盖率可能都测不出来,只能靠代码评审去发现。而且有可能为了将来考虑,就算发现了也不一定会去改。
    dumbbell5kg
        6
    dumbbell5kg  
    OP
       217 天前
    @nothingistrue isDone()也说了 cancel 是 complete 的一种,也就是可能是 cancel 导致走到了 whenComplete 中,为什么说 complete 跟 cancel 是互斥的?

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


    我的疑问点是 whenComplete 里面的 isDone 恒为 true ,那么在 whenComplete 里判断 isDone 不是多余了吗
    dumbbell5kg
        7
    dumbbell5kg  
    OP
       217 天前
    @asssfsdfw isDone 和 isCompletedExceptionally 一起用没什么问题的,isDone=true ,isCompletedExceptionally 也可能=false
    dumbbell5kg
        8
    dumbbell5kg  
    OP
       217 天前
    @nothingistrue
    这里我撤回,是我看错了你的回复
    `正因为 isDone 包含了 isCompletedExceptionally ,才在 isDone 后面判断了是 isDone 的哪种情况(isCompletedExceptionally ),这里我觉得也不多余。`
    asssfsdfw
        9
    asssfsdfw  
       217 天前
    @dumbbell5kg 既然 isCompletedExceptionally()都为 false 了,为什么还要 isDone ?
    dumbbell5kg
        10
    dumbbell5kg  
    OP
       217 天前
    @asssfsdfw 我应该理解错了你的意思,忽略我的第一条回复,isCompletedExceptionally 前的 isDone 确实多余了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2826 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 05:54 · PVG 13:54 · LAX 22:54 · JFK 01:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.