CompletableFuture 多whenComplete操作

1. future 多个whenComplete , 多个whenComplete会执行, 相互间没有影响

    @Test
    public void futureTest(){


        CompletableFuture future = new CompletableFuture();

        future.whenComplete((r, t)->{
            logger.info("when 1, r:{}, t:{}", r, t);
        });
        future.whenComplete((r, t)->{
            logger.info("when 2, r:{}, t:{}", r, t);
            // throw new RuntimeException("1234566");
        }).whenComplete((r, t)->{
            logger.info("when 2+1, r:{}, t:{}", r, t);
        }).thenApply(r ->{
            logger.info("apply run :{}", r);
            return r;
        }).whenComplete((r, t)->{
            logger.info("when 2+2, r:{}, t:{}", r, t);
        });

        future.whenComplete((r, t)->{
            logger.info("when 3, r:{}, t:{}", r, t);
        });

        future.complete(123);

    }
[INFO ] 10:25:21.446[main]c.e.s.t.utils.TestUtils.lambda$futureTest$7:311 -when 3, r:123, t:null
[INFO ] 10:25:21.449[main]c.e.s.t.utils.TestUtils.lambda$futureTest$3:299 -when 2, r:123, t:null
[INFO ] 10:25:21.450[main]c.e.s.t.utils.TestUtils.lambda$futureTest$4:302 -when 2+1, r:123, t:null
[INFO ] 10:25:21.450[main]c.e.s.t.utils.TestUtils.lambda$futureTest$5:304 -apply run :123
[INFO ] 10:25:21.450[main]c.e.s.t.utils.TestUtils.lambda$futureTest$6:307 -when 2+2, r:123, t:null
[INFO ] 10:25:21.450[main]c.e.s.t.utils.TestUtils.lambda$futureTest$2:296 -when 1, r:123, t:null
Disconnected from the target VM, address: '127.0.0.1:10833', transport: 'socket'

2.  future.whenComplete 抛出异常, 不同whenComplete之间无影响

    @Test
    public void futureTest(){


        CompletableFuture future = new CompletableFuture();

        future.whenComplete((r, t)->{
            logger.info("when 1, r:{}, t:{}", r, t);
        });
        future.whenComplete((r, t)->{
            logger.info("when 2, r:{}, t:{}", r, t);
            throw new RuntimeException("1234566");
        }).whenComplete((r, t)->{
            logger.info("when 2+1, r:{}, t:{}", r, t);
        }).thenApply(r ->{
            logger.info("apply run :{}", r);
            return r;
        }).whenComplete((r, t)->{
            logger.info("when 2+2, r:{}, t:{}", r, t);
        });

        future.whenComplete((r, t)->{
            logger.info("when 3, r:{}, t:{}", r, t);
        });

        future.complete(123);

    }
[INFO ] 10:26:01.400[main]c.e.s.t.utils.TestUtils.lambda$futureTest$7:311 -when 3, r:123, t:null
[INFO ] 10:26:01.405[main]c.e.s.t.utils.TestUtils.lambda$futureTest$3:299 -when 2, r:123, t:null
[INFO ] 10:26:01.408[main]c.e.s.t.utils.TestUtils.lambda$futureTest$4:302 -when 2+1, r:null, t:{}
java.util.concurrent.CompletionException: java.lang.RuntimeException: 1234566
[INFO ] 10:26:01.408[main]c.e.s.t.utils.TestUtils.lambda$futureTest$6:307 -when 2+2, r:null, t:{}
java.util.concurrent.CompletionException: java.lang.RuntimeException: 1234566
[INFO ] 10:26:01.409[main]c.e.s.t.utils.TestUtils.lambda$futureTest$2:296 -when 1, r:123, t:null
Disconnected from the target VM, address: '127.0.0.1:10870', transport: 'socket'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值