将CompletableFutures组合成树状结构

用户3001

我目前正在Java 8的Future API和Core CompleteableFuture中进行试验。我知道这是一个monad,因此它应该有一个bind-Operator。

现在的想法是我有一棵抽象运算k的树,例如:

O --- > O3 ---> O4 ---> o5
  O ---> o2 --/

我想“合并”O3O2进行新的操作O4 = (O3 o O2) (t) = O2(O3(t))我的想法是,例如,我O2要说:将当前节点与合并,O3并返回一个由串联操作组成的新节点。不幸的是,我已经整夜尝试了,无法解决。同样,由于未知的原因,使用类似运算符的操作O1.mergeWith(O2).mergeWith(O3)会触发两次调用一次apply方法。

目标是创建一个由其他功能组成的新功能,因此我可以尽可能推迟计算。

public abstract class Operation<T,R> {
  // The value a
  private final CompletableFuture<R> future;

// Executes the operation on t and returns something (maybe different) of type R
  protected abstract R apply(T t);

// Gets the value of the future of this operation
  public R get() throws Exception {
    return future.get();
  }

  protected Operation(Supplier<T> s) {
    future =  CompletableFuture.supplyAsync(s).thenApplyAsync(transform());
  }

  protected Operation(CompletableFuture<R> f) {
    future = f;
  }


  CompletableFuture<R> createTargetFuture(R _value) {
    return CompletableFuture.supplyAsync(() -> _value);
  }

  <S> Operation<T,S> mergeWith(Operation<R,S> _other) {
    CompletableFuture<S> _result = future.thenComposeAsync(
      // Method is synchronous, but is run async :) We need the createTargetFuture,         otherwise we cannot turn the constant t into a producer.
      (t) -> _other.createTargetFuture(_other.apply(t)));
      //transform()).thenApplyAsync(_other.transform());
    return new Operation<T, S>(_result) {
      @Override
      protected S apply(T t) {
        // What to put here
        return null;
      }
    };
  }

  protected Function<T,R> transform(){
    return this::apply;
  }
}
霍尔格

目前尚不清楚您要实现的目标。您的整个Operation类看起来像是要添加一个CompletableFuture它自己已经提供的功能

如果您有一个Supplier<T> s并且想要推迟其执行,可以CompletableFuture.supplyAsync(s)按照您已经知道的方法进行。

现在,如果要将aFunction<T,R> f应用于CompletableFuture<T> a简单使用的延迟结果CompletableFuture.supplyAsync(()->f.apply(a.join()))

对于两个CompletableFutures和一个,相同的工作原理BiFunctionCompletableFuture.supplyAsync(()->f.apply(a.join(),b.join()))

如果您需要将操作包装在某种支持代码中,则可以创建如下的实用程序方法:

public static <T> CompletableFuture<T> create(Supplier<T> s) {
    return CompletableFuture.supplyAsync(s);
}
public static <T,R> CompletableFuture<R> create(
  Function<T,R> f, CompletableFuture<T> a) {
    return CompletableFuture.supplyAsync(()->f.apply(a.join()));
}
public static <T,U,R> CompletableFuture<R> create(
  BiFunction<T,U,R> f, CompletableFuture<T> a, CompletableFuture<U> b) {
    return CompletableFuture.supplyAsync(()->f.apply(a.join(),b.join()));
}

然后,您可以愉快地执行异步操作,例如:

int i=create((a,b)->a+b, create(()->42),create(()->100)).join();

但是,当然,如果没有任何支持方法,同样的事情也会起作用:

int i=CompletableFuture.supplyAsync(() ->
    CompletableFuture.supplyAsync(()->42).join()
  + CompletableFuture.supplyAsync(()->100).join() ).join();

要么

int j=CompletableFuture.supplyAsync(()->42).thenApplyAsync(
    a-> a + CompletableFuture.supplyAsync(()->100).join() ).join();

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将 observables 组合成 observables 数组

来自分类Dev

将谓词组合成表达式

来自分类Dev

将许多对象的输出组合成CSV

来自分类Dev

将数组对象组合成单个对象

来自分类Dev

将数组对象组合成单个对象

来自分类Dev

将内部数组组合成多维数组-PHP

来自分类Dev

将makefile目标组合成更可重用的目标

来自分类Dev

将两个列表组合成python字典

来自分类Dev

将Javascript数组组合成键值查找

来自分类Dev

如何将列向量组合成矩阵

来自分类Dev

使用dplyr将多列组合成矢量

来自分类Dev

将列表列表组合成字典的干净方法?

来自分类Dev

MVC路由-将参数组合成行动?

来自分类Dev

将字符串组合成对(Matlab)

来自分类Dev

将谓词组合成表达式

来自分类Dev

将数组元素组合成不同的数组

来自分类Dev

将文本列表组合成以逗号分隔的6组

来自分类Dev

将字符串组合成C语言的列表

来自分类Dev

RxJS 将 ObjectA 排放组合成 ObjectB 数组

来自分类Dev

递归地将 Rx Singles 组合成 Observables

来自分类Dev

机器学习:将特征组合成单个特征

来自分类Dev

Python Pandas 将多列组合成单列

来自分类Dev

如何将图像组合成堆栈

来自分类Dev

将矩阵元素组合成 4 组,不重复

来自分类Dev

php将变量组合成树枝的数组

来自分类Dev

以反应形式将数组的值组合成 FormArray 行

来自分类Dev

使用硒将多个 for 循环组合成 CSV

来自分类Dev

将多个模块 jar 组合成一个

来自分类Dev

Swift-如何根据匹配值将两个无序结构数组组合成一个新对象数组?