CompletableFuture的thenCompose使用具体说明

1.thenCompose

如果你还没有了解CompletableFuture或者希望再次熟悉一下,可以参考 CompletableFuture使用方法详细说明

1.1. thenCompose的特点

thenCompose方法会在某个任务执行完成后,将该任务的执行结果作为方法入参然后执行指定的方法,该方法会返回一个新的CompletableFuture实例。

也就是对一个CompletableFuture返回的结果进行后续操作,返回一个新的CompletableFuture。

1.2.thenCompose的定义

    public <U> CompletableFuture<U> thenCompose(
        Function<? super T, ? extends CompletionStage<U>> fn) {
        return uniComposeStage(null, fn);
    }

    public <U> CompletableFuture<U> thenComposeAsync(
        Function<? super T, ? extends CompletionStage<U>> fn) {
        return uniComposeStage(asyncPool, fn);
    }

    public <U> CompletableFuture<U> thenComposeAsync(
        Function<? super T, ? extends CompletionStage<U>> fn,
        Executor executor) {
        return uniComposeStage(screenExecutor(executor), fn);
    }

可以看到方法的返回值CompletionStage<U>,重点就是需要理解它们的传入参数fn。接下来和thenApply进行对比说明。

2.thenApply与thenCompose的异同

thenApply和thenCompose都是对一个CompletableFuture返回的结果进行后续操作,返回一个新的CompletableFuture。

不同的是两个方法的参数不同,上面我们已经看到了thenCompose的方法定义,下面我们看看thenApply的方法定义

	public <U> CompletableFuture<U> thenApply(
        Function<? super T,? extends U> fn) {
        return uniApplyStage(null, fn);
    }

    public <U> CompletableFuture<U> thenApplyAsync(
        Function<? super T,? extends U> fn) {
        return uniApplyStage(asyncPool, fn);
    }

    public <U> CompletableFuture<U> thenApplyAsync(
        Function<? super T,? extends U> fn, Executor executor) {
        return uniApplyStage(screenExecutor(executor), fn);
    }

很明显,和thenCompose方法对比,两个方法的返回值都是CompletionStage,不同之处在于它们的传入参数。

  • thenApply:fn函数是一个对一个已完成的stage或者说CompletableFuture的的返回值进行计算、操作,也就是说转换的是泛型中的类型,相当于将CompletableFuture 转换生成新的CompletableFuture
  • thenCompose:fn函数是对另一个CompletableFuture进行计算、操作,也就是说用来连接两个CompletableFuture,是生成一个新的CompletableFuture。

上面的理解吗?大白话说就是thenApply从 CompletableFuture<T> 生成新的CompletableFuture<U>,只是将CompletableFuture的T类型转换为了U类型而已,CompletableFuture还是同一个CompletableFuture。

而thenCompose是生成了一个新的CompletableFuture。

3.示例

上面只是进行了说明,接下里进行代码示例配合说明,更容易理解

thenApply示例:

    @Test
    public void test() throws ExecutionException, InterruptedException {
        /*
        如果supplyAsync直接返回, 得到CompletableFuture<String>
        现在经过了thenApply的处理, CompletableFuture<String> 转换为 CompletableFuture<Integer>, CompletableFuture是同一个
         */
        final CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            // 先执行supplyAsync方法,得到返回值
            return "hello";
        }).thenApply(value -> {
            // 接收到supplyAsync的返回值“hello”
            if ("hello".equals(value)) {
                return 111;
            } else {
                return 000;
            }
        });

        final Integer integer = completableFuture.get();
        System.out.println("结果:" + integer);
    }

执行结果:

结果:111

thenCompose示例:

    @Test
    public void test() throws ExecutionException, InterruptedException {
        //
        final CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            // 先执行supplyAsync方法,得到返回值
            return "hello";
        }).thenCompose(value -> CompletableFuture.supplyAsync(() -> {
            // thenCompose方法返回一个新的CompletableFuture
            if ("hello".equals(value)) {
                return 111;
            } else {
                return 000;
            }
        }));

        final Integer integer = completableFuture.get();
        System.out.println("结果:" + integer);
    }

执行结果:

结果:111

总结:thApply和thenCompose都是将一个CompletableFuture<String>转换为CompletableFuture<Integer>。

不同的是,thenApply中的传入函数的返回值是String,而thenCompose的传入函数的返回值是CompletableFuture<Integer>。

posted @ 2022-08-31 13:30  KILLNPE  阅读(212)  评论(0编辑  收藏  举报