繁体   English   中英

等待 Completable 未来线程完成的推荐方法是什么

[英]What is the recommended way to wait till the Completable future threads finish

我在代码中使用CompletableFuture ,如下所示。 但是关于我应该等到所有 runnables 完成的方式,我找到了两种方法,我不知道它们之间的区别,哪一种是最佳实践? 它们如下:

代码

this.growSeedFutureList = CompletableFuture.runAsync(new GrowSeedSERun(this.saliencyMat, this.seedXY, this.seedVal), this.growSeedExecutor);
this.growSeedFutureList = CompletableFuture.runAsync(new GrowSeedNWRun(this.saliencyMat, this.seedXY, this.seedVal), this.growSeedExecutor);
this.growSeedFutureList = CompletableFuture.runAsync(new GrowSeedNERun(this.saliencyMat, this.seedXY, this.seedVal), this.growSeedExecutor);
this.growSeedFutureList = CompletableFuture.runAsync(new GrowSeedSWRun(this.saliencyMat, this.seedXY, this.seedVal), this.growSeedExecutor);

等待所有可运行对象完成的第一种方法

this.growSeedExecutor.shutdown();
this.growSeedExecutor.awaitTermination(1, TimeUnit.DAYS);

等待所有可运行对象完成的第二种方法

CompletableFuture.allOf(this.growSeedFutureList).join();

请告诉我推荐哪一个。

如果你真的想等待所有的期货,你可以简单地在每个期货上调用join()

growSeedFutureList.forEach(CompletableFuture::join);

与使用allOf()相比,主要区别在于,一旦到达具有异常的未来,它将抛出异常,而allOf().join()版本将仅在所有期货完成后抛出异常(异常或不)。

另一个小的区别是,这并没有创建allOf阶段的中介。 如果您想在所有期货完成后异步执行某些操作,而不是等待所有期货完成,那么此阶段仍然有用。

另一方面,执行程序的解决方案有几个缺点:

  • 它会阻止重用执行程序,因为它需要关闭它;
  • 它要求您对所有操作使用该执行程序 - 它不适用于以其他方式管理的CompletableFuture ;
  • 它没有清楚地表明你的意图,即等待所有的期货完成;
  • 实施起来比较复杂;
  • 它不处理异常完成 - 如果其中一个任务失败,则awaitTermination()不会抛出任何异常。

只有当执行程序(growSeedExecutor)仅用于给定任务时,两种方式才是等效的。 第一种方式可能导致以下:另一个任务需要并行化,并为每个任务创建新的执行程序。 一些开发人员看到创建了太多执行程序,并决定使用单个通用执行程序,但未能删除所有执行程序关闭...

所以第二种方式(join())更可靠,因为它不那么复杂。 但是每个新的未来都应该添加到growSeedFutureList中,而不是分配给它。

回复有点晚了,但希望这段代码能帮助别人看。 这使用了常见的 forkJoin 池执行器

package com.company;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class CompletableFutureExample {
    public static void main(String args[]){
        List<CompletableFuture> futureList=new ArrayList<>();
        for(int i=0;i<10;i++) {
            futureList.add(CompletableFuture.supplyAsync(()->getThreadName()).thenAccept(name->printThreadName(name)));
        }
        futureList.forEach(CompletableFuture::join);
    }

    static String getThreadName(){
        String threadDetails=Thread.currentThread().getName();
        System.out.println("thread deteails::::"+threadDetails);
        return threadDetails;
    }
    static void printThreadName(String value){
        System.out.println("thread string value::"+value);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM