简体   繁体   English

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

[英]Java 8 - How to track number of exceptions invoked within an async parallel stream using CompletableFuture

Sorry for the confusing title, what I am trying to is track the number of times a method that is executed asynchronously has thrown an exception, while also retrieving the results that executed successfully into a class variable.抱歉,标题令人困惑,我正在尝试跟踪异步执行的方法引发异常的次数,同时还将成功执行的结果检索到类变量中。 I think my implementation is quite off though, would a List of CompletableFutures be more appropriate here than a CompletableFuture of a List?不过,我认为我的实现很不合适,CompletableFuture 的列表在这里比列表的 CompletableFuture 更合适吗?

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());
    }
}

So looking at your code you will only ever get at most 1 exception.因此,查看您的代码,您最多只会收到 1 个异常。 A better CompletableFuture call for each call to addNumber.对 addNumber 的每次调用都有一个更好的 CompletableFuture 调用。 Then check whether it is exceptional.然后检查是否异常。

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));
}

Here I have submitted each call to addNumber asynchronously and waited to join them all after they are completed by using allOf这里我已经异步提交了对addNumber每个调用,并在它们完成后使用allOf等待加入它们

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

相关问题 Spring Async、Async with CompletableFuture 和 Java8 并行 stream 之间的区别 - Difference between Spring Async, Async with CompletableFuture and Java8 parallel stream 使用Stream API或CompletableFuture的并行数据库查询? - Parallel db queries using Stream API or CompletableFuture? 使用Java 8流和CompletableFuture的并行数据库调用 - Parallel database calls using Java 8 streams and CompletableFuture 并行流和CompletableFuture之间的差异 - Difference between parallel stream and CompletableFuture Java 8 CompletableFuture,流和超时 - Java 8 CompletableFuture , Stream and Timeouts 如何在Java 8 Stream.flatMap(..)中捕获异常 - How to catch exceptions within Java 8 Stream.flatMap(..) 如何使用 CompletableFuture 将此代码转换为 Java 8 中的异步代码? 我正在使用 Spring 引导 - How to convert this code to async in Java 8 using CompletableFuture ? I am using Spring Boot 使用Java中的CompletableFuture并行执行for循环并记录执行 - Execute a for loop in parallel using CompletableFuture in Java and log the execution 如何将1个可完成的未来分成流中的许多可完成的未来? - How to divide 1 completablefuture to many completablefuture in stream? 如何在Spring Boot中使用CompletableFuture并行调用方法多次 - How to call a method multiple times in parallel using CompletableFuture in Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM