CommpetableFuture使用anyOf过程中的一些优化思考

简介: CommpetableFuture使用anyOf过程中的一些优化思考

系列目录:

  1. Spring WebFlux运用中的思考与对比
  2. CompletableFuture与Spring的Sleuth结合工具类
  3. CommpetableFuture使用anyOf过程中的一些优化思考
  4. 结合CompletableFuture与Spring的Sleuth结合工具类与allOf以及anyOf


上一篇我们讲述了如何将CompletableFuture与Spring Sleuth结合起来。这篇我们继续优化


CompletableFuture


CompletableFuture的allOf


首先我们看看allOf的定义:

public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) {
    // ...
}

这个方法接受若干个返回不同类型的CompletableFuture为参数, 返回一个返回为空(Void)的CompletableFuture。也就是说,这个方法其实就是返回一个在所有参数完成之后也完成的返回为空(Void)的CompletableFuture,也就是充当一个signaling device

这个方法很好,尤其是并发获取多种io的结果的时候。但是用这个方法,带来了很多不便,最大的不便就是,返回是Void,而不是所有的参数的返回。这样导致我们,需要在聚合这些结果的那个服务方法里面,把最终结果封装好,否则,获取的就是一个Void。举个例子:

假设我的一个服务方法的返回是多个接口在使用,这个方法需要同时调用三个io等待他们都返回时,利用这三个io的返回,拼装成接口需要的字段。对于这个场景,我们可以有两种写法,第一种是基于回调的写法,第二种是基于返回的写法,两种都OK,看个人习惯,我个人倾向于基于返回的写法,这样代码是瀑布式的,基于回调的会导致多层嵌套,导致代码可读性降低。

** 结果类:**

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class Result {
   private String string;
   private List<String> strings;
   private List<Integer> integers;
}

** 基于回调:**

public static void baseOnCallBack(CompletableFuture<Result> resultCompletableFuture) {
   CompletableFuture<List<String>> result1 = CompletableFuture.supplyAsync(() -> {
       //模拟io
       try {
           TimeUnit.MILLISECONDS.sleep(ThreadLocalRandom.current().nextLong(1000));
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
       return Lists.newArrayList("a", "b", "c");
   });
   CompletableFuture<List<Integer>> result2 = CompletableFuture.supplyAsync(() -> {
       //模拟io
       try {
           TimeUnit.MILLISECONDS.sleep(ThreadLocalRandom.current().nextLong(1000));
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
       return Lists.newArrayList(1, 2, 3);
   });
   CompletableFuture<String> result3 = CompletableFuture.supplyAsync(() -> {
       //模拟io
       try {
           TimeUnit.MILLISECONDS.sleep(ThreadLocalRandom.current().nextLong(1000));
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
       return "hash-test";
   });
   CompletableFuture.allOf(result1, result2, result3).thenAcceptAsync(v -> {
       resultCompletableFuture.complete(Result.builder()
               //一定存在的,因为已经完成了
               .string(result3.join())
               .strings(result1.join())
               .integers(result2.join())
               .build());
   });
}

** 基于返回:**

public static CompletableFuture<Result> baseOnReturn() {
   CompletableFuture completableFuture = new CompletableFuture();
   CompletableFuture<List<String>> result1 = CompletableFuture.supplyAsync(() -> {
       //模拟io
       try {
           TimeUnit.MILLISECONDS.sleep(ThreadLocalRandom.current().nextLong(1000));
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
       return Lists.newArrayList("a", "b", "c");
   });
   CompletableFuture<List<Integer>> result2 = CompletableFuture.supplyAsync(() -> {
       //模拟io
       try {
           TimeUnit.MILLISECONDS.sleep(ThreadLocalRandom.current().nextLong(1000));
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
       return Lists.newArrayList(1, 2, 3);
   });
   CompletableFuture<String> result3 = CompletableFuture.supplyAsync(() -> {
       //模拟io
       try {
           TimeUnit.MILLISECONDS.sleep(ThreadLocalRandom.current().nextLong(1000));
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
       return "hash-test";
   });
   CompletableFuture.allOf(result1, result2, result3).thenAcceptAsync(v -> {
       completableFuture.complete(Result.builder()
               //一定存在的,因为已经完成了
               .string(result3.join())
               .strings(result1.join())
               .integers(result2.join())
               .build());
   });
   return completableFuture;
}


基于回调的接口使用结果:

CompletableFuture completableFuture = new CompletableFuture();
baseOnCallBack(completableFuture);
completableFuture = completableFuture.thenAcceptAsync(result -> {
    System.out.println("baseOnCallback: " + result);
});


基于返回的接口使用结果:

CompletableFuture<Void> voidCompletableFuture = baseOnReturn().thenAcceptAsync(result -> {
    System.out.println("baseOnReturn: " + result);
});

可以看出,一层嵌套也是基于返回的代码看上去更优雅。

我们再来思考下,如果allOf中的所有CompletableFuture都返回的是同一个类型的结果,例如String,那么可不可以让allOf直接返回List<String>呢?

我们可以将一个allOf变成多个allOf这么实现:

public static <T> CompletableFuture<List<T>> allOf(Collection<CompletableFuture<T>> futures) {
    return futures.stream().collect(Collectors.collectingAndThen(
                    Collectors.toList(),
                    l -> CompletableFuture.allOf(l.toArray(new CompletableFuture[0]))
                            .thenApply(v -> l.stream().map(CompletableFuture::join).collect(Collectors.toList()))
            )
    );
}
相关文章
|
1月前
|
数据采集 JSON 数据可视化
colnames看似简单,却能优化数据处理流程
本文介绍如何使用R语言的`colnames`函数优化爬虫数据处理流程,以采集BOSS直聘招聘信息为例。通过设置合理的列名,提升数据可读性和分析效率。具体步骤包括配置代理IP、发起HTTP请求、解析JSON数据并保存为CSV文件。进一步分析薪资、岗位和公司热度,助力业务决策。示例代码展示了从数据采集到可视化的完整过程。
67 17
|
3月前
|
算法 调度
多级反馈队列算法的具体实现过程是怎样的?
【10月更文挑战第25天】多级反馈队列算法通过动态调整进程的优先级和在不同优先级队列之间的转移,能够较好地适应不同类型进程的需求,兼顾了短作业优先、I/O密集型作业优先等多种调度策略的优点,提高了系统的整体性能和资源利用率,同时也能保证对实时性要求较高的进程能够及时得到响应。
101 4
|
6月前
数据平台问题之在数据影响决策的过程中,如何实现“决策/行动”阶段
数据平台问题之在数据影响决策的过程中,如何实现“决策/行动”阶段
|
内存技术
AS3使用过程中问题总结
AS3使用过程中问题总结
73 0
优化if-else代码的几种方案
优化if-else代码的几种方案
Sub过程
参数表是用来指明调用该Sub过程时需要传递给该过程的参数及类型。表内的参数称为形参。Sub过程可以没有形参(但小括号不可以省略),也可1到多个形参(多个之间用逗号隔开);
Sub过程
一道优化过程的题
一道优化过程的题
110 0
代码中大量的if/else,你有什么优化方案?
代码中大量的if/else,你有什么优化方案?
353 0
代码中大量的if/else,你有什么优化方案?
|
NoSQL Ubuntu MongoDB
使用过程心得
一些常用操作和常见问题
使用过程心得