Java 8-如何使用CompletableFuture跟踪异步并行流中调用的异常数

Java 8-如何使用CompletableFuture跟踪异步并行流中调用的异常数,java,java-8,completable-future,Java,Java 8,Completable Future,很抱歉让人困惑的标题,我试图跟踪异步执行的方法引发异常的次数,同时将成功执行的结果检索到类变量中。我认为我的实现已经相当失败了,一个完整的未来列表在这里会比一个完整的未来列表更合适吗 public class testClass { private List<Integer> resultNumbers; public void testMethod() { int exceptions = 0; try {

很抱歉让人困惑的标题,我试图跟踪异步执行的方法引发异常的次数,同时将成功执行的结果检索到类变量中。我认为我的实现已经相当失败了,一个完整的未来列表在这里会比一个完整的未来列表更合适吗

public class testClass {

    private List<Integer> resultNumbers;

    public void testMethod() {

        int exceptions = 0;
        try {
            methodWithFuture();
        catch (InterruptedException | ExecutionException e) {
            exceptions++;
        }
        System.out.println("Number of times the addNumber method threw an exception=" + exceptions);
    }

    public void methodWithFuture() throws InterruptedException, ExecutionException {

        List<Integer> numbersList = Arrays.asList(new Integer[] { 1, 2, 3 })
        CompletableFuture<List<Integer>> futuresList = CompletableFuture.supplyAsync(() -> 
            numbersList.parallelStream().map(number -> addNumber(number))).collect(Collectors.toList()),
            new ForkJoinPool(3));

        resultNumbers.addAll(futuresList.get());
    }
}
公共类testClass{
私有列表结果编号;
公共void testMethod(){
int异常=0;
试一试{
方法与未来();
捕获(中断异常|执行异常e){
例外情况++;
}
System.out.println(“addNumber方法引发异常的次数=”+异常);
}
public void methodWithFuture()引发InterruptedException、ExecutionException{
List numbersList=Arrays.asList(新整数[]{1,2,3})
CompletableFuture未来列表=CompletableFuture.supplyAsync(()->
numbersList.parallelStream().map(number->addNumber(number)).collect(collector.toList()),
新泳池(3);;
resultNumbers.addAll(futuresList.get());
}
}

因此,查看您的代码,您最多只能得到一个异常。对于addNumber的每个调用,都会有一个更完整的未来调用。然后检查它是否异常

public void testMethod(){

    int exceptions = 0;

    List<Integer> numbersList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
    List<CompletableFuture<Integer>> cfList = new ArrayList<>();

    for(int number : numbersList){
        CompletableFuture<Integer> cf = methodWithFuture(number);
        cfList.add(cf);
    }

    CompletableFuture<Void> allOfCF = CompletableFuture.allOf(cfList.toArray(new CompletableFuture[0]));       
    try {allOf.get();} catch (InterruptedException | ExecutionException ignored) {}

    int sum = 0;
    for(CompletableFuture<Integer> cf : cfList){
        if(cf.isCompletedExceptionally()){
            exceptions ++;
        } else {
            sum += cf.get();
        }
    }

    System.out.println("Number of times the addNumber method threw an exception=" + exceptions);
    System.out.println("SUM " + sum);
}


public CompletableFuture<Integer> methodWithFuture(int number) {
    return CompletableFuture.supplyAsync(() -> addNumber(number));
}
public void testMethod(){
int异常=0;
List numbersList=Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13);
List cfList=new ArrayList();
对于(整数编号:numbersList){
CompletableFuture cf=methodWithFuture(编号);
cfList.add(cf);
}
CompletableFuture allOfCF=CompletableFuture.allOf(cfList.toArray(新的CompletableFuture[0]);
尝试{allOf.get();}catch(InterruptedException | ExecutionException被忽略){}
整数和=0;
对于(可完成的未来cf:cfList){
if(参见isCompletedExceptionally()){
例外情况++;
}否则{
sum+=cf.get();
}
}
System.out.println(“addNumber方法引发异常的次数=”+异常);
系统输出打印项次(“总和”+总和);
}
公共完整未来方法WithFuture(整数){
返回CompletableFuture.supplyAsync(()->addNumber(number));
}

在这里,我异步提交了每个对
addNumber
的调用,并在使用
allOf

完成后等待加入它们,因此查看您当前的代码,您最多只能得到一个异常