Java CompletableFuture然后由几个异步任务组成

我有一个由2个异步步骤组成的过程。第二步基于第一步的结果运行。该过程是循环启动的。挑战在于,第二步是由多个异步任务完成的,这些任务将第一步迭代的输出作为结果。第一步完成后,我想使用此第一步结果启动n秒步骤。我使用cansnifferCompletableFuture编写了这段代码。

它可以工作,但是我觉得它很复杂,我想知道这是否是正确的方法。我特别想知道,管理第二级子任务和使用thenCompose使其像一个CompletableFuture.allOf一样是否是正确的方法。

CompletableFuture
weareboy123 回答:Java CompletableFuture然后由几个异步任务组成

当您可以使用直接的CompletableFuture.supplyAsync链接相同的依赖函数时,不要在传递给thenCompose的函数中执行thenApplyAsync

通过thenApplyAsync绑定从属函数可以让您在完成第一步之前获取代表这些步骤的CompletableFuture实例,因此您可以将它们全部收集到List中以等待它们的完成。结束时完成,不需要通过CompletableFuture.allOf创建复合期货。

public void test() {
    // Gather CompletableFutures to wait for them at the end
    List<CompletableFuture<?>> futures = new ArrayList<>();

    for (int i = 0; i < 10; i++) {
        int finalI = i;
        CompletableFuture<String> step1 = CompletableFuture.supplyAsync(() -> {
            logger.debug("Start step 1 - " + finalI);
            simulateLongProcessing();// just waits for 1 s
            logger.debug("End step 1 - " + finalI);
            return "step1 output - " + finalI;
        });
        // Second step : Chain several sub-tasks based on the result of the first step
        for (int j = 0; j < 50; j++) {
            final int finalJ = j;
            futures.add(step1.thenApplyAsync(s -> {
                logger.debug("Start - step 2 : " + s + " | " + finalJ);
                simulateLongProcessing();
                logger.debug("End - step 2 : " + s + " | " + finalJ);
                return "step2 output - " + s + " | " + finalJ;
            }));
        }
    }

    // Wait for the completion
    for (CompletableFuture<?> future : futures) {
        future.join();
    }
}
本文链接:https://www.f2er.com/3100625.html

大家都在问