需求
小白吃完饭后要求服务员开发票, 这个时候小白接到了,回家开黑的电话,服务员开好发票后,小白拿着回家了
需求点: 服务员开发票需要异步执行
实现
编写代码
@Testpublic void testFour(){print("小白吃好了");print("小白 结账,要求开发票");CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {print("服务员 收款500元");sleep(100);print("服务员 开发票 面额500元");sleep(200);return "500元发票";});print("小白接到朋友电话,约回家开黑");print(String.format("小白拿到%s, 准备回家", cf1.join()));}
执行结果
1649434740795 | 1 | main | 小白吃好了1649434740795 | 1 | main | 小白 结账,要求开发票1649434740797 | 1 | main | 小白接到朋友电话,约回家开黑1649434740798 | 24 | ForkJoinPool.commonPool-worker-19 | 服务员 收款500元1649434740906 | 24 | ForkJoinPool.commonPool-worker-19 | 服务员 开发票 面额500元1649434741109 | 1 | main | 小白拿到500元发票, 准备回家
需求进阶
应为服务员只负责收款,而不负责开发票,所以需要收到款后去找柜台
需求点: 服务员找柜台开票
实现
编写代码
@Test
public void testFive(){
    print("小白吃好了");
    print("小白 结账,要求开发票");
    CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {
        print("服务员 收款500元");
        sleep(100);
        return "500元";
    }).thenApply(money -> {
        print(String.format("柜台 开发票 面额%s", money));
        sleep(200);
        return String.format("%s发票", money);
    });
    print("小白接到朋友电话,约回家开黑");
    print(String.format("小白拿到%s, 准备回家", cf1.join()));
}
执行结果
1649435142655    |    1    |    main    |    小白吃好了
1649435142655    |    1    |    main    |    小白 结账,要求开发票
1649435142658    |    1    |    main    |    小白接到朋友电话,约回家开黑
1649435142659    |    24    |    ForkJoinPool.commonPool-worker-19    |    服务员 收款500元
1649435142766    |    24    |    ForkJoinPool.commonPool-worker-19    |    柜台 开发票 面额500元
1649435142967    |    1    |    main    |    小白拿到500元发票, 准备回家
显然这个案例和上一章节的案例类似,可以看出thenApply和thenCompose方法类似
异步改造
只需要将方法thenApply->thenApplyAsync即可
改造完成后测试,会变成两个线程,但是我的还是一样,不知道什么鬼,就不粘贴了
需求延续
小白走出餐厅后,来到公交站,准备坐车回家,有两个路线都能回家,一个是700路,一个是800路,现在小白决定,那个先来就做那个
需求点: 如何让小白那个 先来坐那个
实现
编写代码
@Test
public void testSix(){
    print("小白走出餐厅 来到公交站");
    print("等待700路 或者 800路 公交到来");
    CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {
        print("700路公交正在赶来,还需要100毫秒");
        sleep(100);
        return "700路";
    }).applyToEither(CompletableFuture.supplyAsync(() -> {
        print("800路公交正在赶来,还需要200毫秒");
        sleep(200);
        return "800路";
    }), first -> first);
    print(String.format("小白坐%s回家", cf1.join()));
}
执行结果
1649436426844    |    1    |    main    |    小白走出餐厅 来到公交站
1649436426844    |    1    |    main    |    等待700路 或者 800路 公交到来
1649436426848    |    24    |    ForkJoinPool.commonPool-worker-19    |    800路公交正在赶来,还需要200毫秒
1649436426848    |    25    |    ForkJoinPool.commonPool-worker-5    |    700路公交正在赶来,还需要100毫秒
1649436426955    |    1    |    main    |    小白坐700路回家
毫无疑问700先来,做700回家了, 如果有疑问的可以将等待时间对换一下,小白就应该坐800回家
需求延续
小白坐上了700路公交, 又拿起了电话跟朋友聊得正起劲, 公交哐当一下撞树上了…, 谁知道司机在想啥,小白丝毫不慌,从车上下来,在路边拦了个出租车,叫到出租车后,小白顺利回家
需求点: 处理出租车撞树的异常
实现
编写代码
@Test
public void testSeven(){
    print("小白走出餐厅 来到公交站");
    print("等待700路 或者 800路 公交到来");
    CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {
        print("700路公交正在赶来,还需要100毫秒");
        sleep(100);
        return "700路";
    }).applyToEither(CompletableFuture.supplyAsync(() -> {
        print("800路公交正在赶来,还需要200毫秒");
        sleep(200);
        return "800路";
    }), first -> {
        print(first);
        // 如果小白坐上了700路
        if(first.startsWith("700")){
            throw new RuntimeException("700路中途撞树上了......");
        }
        return first;
    }).exceptionally(e -> {
        print(e.getMessage());
        print("小白叫到了出租车");
        return "出租车";
    });
    print(String.format("小白坐%s回家", cf1.join()));
}
执行结果
1649436609713    |    1    |    main    |    小白走出餐厅 来到公交站
1649436609713    |    1    |    main    |    等待700路 或者 800路 公交到来
1649436609716    |    24    |    ForkJoinPool.commonPool-worker-19    |    800路公交正在赶来,还需要200毫秒
1649436609716    |    25    |    ForkJoinPool.commonPool-worker-5    |    700路公交正在赶来,还需要100毫秒
1649436609822    |    25    |    ForkJoinPool.commonPool-worker-5    |    700路
1649436609823    |    25    |    ForkJoinPool.commonPool-worker-5    |    java.lang.RuntimeException: 700路中途撞树上了......
1649436609823    |    25    |    ForkJoinPool.commonPool-worker-5    |    小白叫到了出租车
1649436609823    |    1    |    main    |    小白坐出租车回家
总结
| 方法 | 描述 | 
|---|---|
| thenApply / thenApplyAsync | 和thenCompose的作用大同小异 | 
| applyToEither | 比较两个线程那个优先运行完成,就用那个结果 | 
| exceptionally | 处理前面执行中发生的异常 | 
怎么样,奇怪的知识又增加了
