在vert.x中按顺序减少Future

时间:2018-07-25 07:57:28

标签: java promise future vert.x

在我的项目中,我使用Java中vert.x的Future实现。到目前为止,一切都很好。但是,目前我在顺序执行对象列表上的操作时遇到问题。问题在于reduce方法在Java中分别“减少”和“合并”结果。这导致所有动作同时开始。在easy方法中可以看到,实现顺序执行是可能的。

private Future<Void> action(String object) {
    System.out.println("started " + object);
    Future<Void> f = Future.future();
    vertx.setTimer(1000, res -> {
        System.out.println("doing " + object);
        f.complete();
    });
    return f;
}

private void easy() {
    action("one")
        .compose(ignore -> action("two"))
        .compose(ignore -> action("three"))
        .setHandler(ignore -> System.out.println("completed"));
}

private void list() {
    List<String> l = new ArrayList<>();
    l.add("one");
    l.add("two");
    l.add("three");

    Future<Void> f = Future.future();
    l.stream().reduce(f,
        (f1, s) -> action(s),
        (f1, f2) -> f2.compose(ignore -> f1)
    ).setHandler(res -> {
        System.out.println("completed");
    });
}

执行简单时的输出:

started one
doing one
started two
doing two
started three
doing three
completed

执行列表时的输出:

started one
started two
started three
doing one
doing two
doing three
completed

JavaScript中的相同代码段可以正常工作,因为reduce函数可以一步一步地完成合并操作:

function action(object) {
  return new Promise((resolve, reject) => {
    console.log("started " + object)
    window.setTimeout(() => {
      console.log("doing " + object);
      resolve()
    }, 1000);
  });
}

function easy() {
  action("one")
    .then(() => action("two"))
    .then(() => action("three"))
    .then(() => console.log("completed"));
}

function list() {
  l = ["one", "two", "three"]
  l.reduce((p, s) => p.then(() => action(s)), Promise.resolve())
    .then(() => console.log("completed"));
}

// easy()
list()

easylist的输出与Java代码的简便方法相同。我正在寻找的是一种修复Java中的reduce方法的方法,或者是获得相同结果的另一种方法。

1 个答案:

答案 0 :(得分:0)

好的。我找到了foldLeft方法here的实现,现在顺序执行可以正常工作了……

private void list() {
    List<String> l = new ArrayList<>();
    l.add("one");
    l.add("two");
    l.add("three");

    Future<Void> f = Future.succeededFuture();

    foldLeft(l.iterator(), f,
        (f1, s) -> f1.compose(ignore -> action(s)))
        .setHandler(res -> {
            System.out.println("completed");
        });
}

private static <A, B> B foldLeft(Iterator<A> iterator, B identity, BiFunction<B, A, B> bf) {
    B result = identity;
    while(iterator.hasNext()) {
        A next = iterator.next();
        result = bf.apply(result, next);
    }
    return result;
}