传递给CompletableFuture.allOf()的所有期货都会运行吗? - java

因此,我有一些要运行的期货,即使有些失败,我希望所有人都有机会运行。所以,如果我这样做:

CompletableFuture.allOf(futures).join()

会是这样吗?我的理由是,每个 future 都将在其执行程序中有其自己的可查询的工作,因此,只要主线程没有首先完成,一切都会运行。我的问题是我在.join()上专门指定了.allOf(),因此我的应用程序在运行所有内容之前不会结束

因此allOf()语义使我感到困惑:如果所有成功通过的期货均成功完成,则期货返回是否会完成?还是如果看到一个失败而没有等待其余的失败,它将完成一个失败的 future 吗?

编辑

为了进一步说明我的问题,.allOf是否表现如下:

Stream.of(futures).forEach(future -> {
  try {
    future.join()
  } catch (Throwable e) {
    //dont throw, we want to join the rest
  }
})

还是表现如下:

Stream.of(futures).forEach(future -> {
  try {
    future.join()
  } catch (Throwable e) {
    throw e; //All other remaining .join() wont run
  }
})

哪有第一种还是第二种情况?由于我希望第一种情况是我暂时在代码中使用的,但是我想尽可能使用allOf(),因为它更美观

谢谢!

参考方案

是的,每个 future 都将独立尝试完成。

我认为您也在尝试了解控件在各种情况下的流动方式。我提出了4种方案:

  • 由于未处理的异常
  • 而导致失败的 future

  • 一个将来必须明确标记为失败的future,并带有completeExceptionally并且在其尾部有一个例外地块。
  • 必须显式标记为future且带有completeExceptionally并且未在其尾部具有异常阻止的future。
  • 一个可以成功完成的 future 。
  • //CASE 1
    // A future that shall fail due to an unandled exception in its run 
    // and has an exceptionally block at its tail
    CompletableFuture<Void> unhandledFailureFutureWithExceptionHandler =
        CompletableFuture.runAsync(() -> {
            throw new RuntimeException("Exception in unhandledFailureFutureWithExceptionHandler");
        });
    unhandledFailureFutureWithExceptionHandler = unhandledFailureFutureWithExceptionHandler
        .exceptionally(throwable -> {
            // Handling exception for this future
            // HANDLING POINT 1
            System.out.println("Handling exception at HANDLING POINT FOR CASE 1, 
                failure message is : " + throwable.getMessage());
            return null;
        });
    
    //CASE 2
    //A future that shall fail and has an exceptionally block at its tail
    CompletableFuture<Void> failedFutureWithExceptionHandler = new CompletableFuture<>();
    failedFutureWithExceptionHandler.completeExceptionally(
        new RuntimeException("Exception in failedFutureWithExceptionHandler")
    );
    failedFutureWithExceptionHandler = failedFutureWithExceptionHandler.exceptionally(throwable -> {
        // Handling exception for this future
        // HANDLING POINT 2
        System.out.println("Handling exception at HANDLING POINT FOR CASE 2, 
            failure message is : " + throwable.getMessage());
        return null;
    });
    
    //CASE 3
    //A future that shall fail and has no exceptionally block at its tail
    CompletableFuture<Void> failedFutureWithoutExceptionHandler = new CompletableFuture<>();
    failedFutureWithoutExceptionHandler.completeExceptionally(
        new RuntimeException("Exception in failedFutureWithoutExceptionHandler")
    );
    
    //CASE 4
    //A future that shall succeed and print a message to console
    CompletableFuture<Void> successFuture = CompletableFuture.runAsync(() -> 
        System.out.println("CASE 4 : Running successFuture")
    );
    
    CompletableFuture.allOf(unhandledFailureFutureWithExceptionHandler, 
        failedFutureWithExceptionHandler, failedFutureWithoutExceptionHandler, successFuture)
            .exceptionally(throwable -> {
                // Handling exception if ANY of the futures that did not have its own exceptionally block
                // In this case the exception of `failedFutureWithoutExceptionHandler` will be handled here
                // HANDLING POINT 3
                System.out.println("Handling exception at HANDLING POINT FOR CASE 3, 
                    failure message is : " + throwable.getMessage());
                return null;
            }).join();
    

    控制台上产生的输出是

    Handling exception at HANDLING POINT FOR CASE 1, failure message is : java.lang.RuntimeException: Exception in unhandledFailureFutureWithExceptionHandler
    Handling exception at HANDLING POINT FOR CASE 2, failure message is : Exception in failedFutureWithExceptionHandler
    CASE 4 : Running successFuture
    Handling exception at HANDLING POINT FOR CASE 3, failure message is : java.lang.RuntimeException: Exception in failedFutureWithoutExceptionHandler
    

    如您所见,将来是否会像情况1一样抛出未处理的错误,如果它的尾部链接了exceptionally块,则应在此时处理异常

    对于情况2,如果将来用completeExceptionally标记为future,则如果future的尾部链接了一个处理程序,则exceptionally块应由该块处理。

    在情况3中,将来被标记为失败,并且没有异常块,因此应由下一级的exceptionally块处理,在这种情况下,它是exceptionallyallOf()块。

    如您所见,案例4即将完成,并且无论其他期货是否出现故障,消息都会在控制台上打印。

    Java:正则表达式模式匹配器是否有大小限制? - java

    我的模式类似于OR:“word1 | word2 | word3”我大约有800个字。可能有问题吗? 参考方案 您仅受记忆和理智的限制。 :)

    Java:线程池如何将线程映射到可运行对象 - java

    试图绕过Java并发问题,并且很难理解线程池,线程以及它们正在执行的可运行“任务”之间的关系。如果我创建一个有10个线程的线程池,那么我是否必须将相同的任务传递给池中的每个线程,或者池化的线程实际上只是与任务无关的“工人无人机”可用于执行任何任务?无论哪种方式,Executor / ExecutorService如何将正确的任务分配给正确的线程? 参考方案 …

    JAVA:字节码和二进制有什么区别? - java

    java字节代码(已编译的语言,也称为目标代码)与机器代码(当前计算机的本机代码)之间有什么区别?我读过一些书,他们将字节码称为二进制指令,但我不知道为什么。 参考方案 字节码是独立于平台的,在Windows中运行的编译器编译的字节码仍将在linux / unix / mac中运行。机器代码是特定于平台的,如果在Windows x86中编译,则它将仅在Win…

    java:继承 - java

    有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…

    Java:BigInteger,如何通过OutputStream编写它 - java

    我想将BigInteger写入文件。做这个的最好方式是什么。当然,我想从输入流中读取(使用程序,而不是人工)。我必须使用ObjectOutputStream还是有更好的方法?目的是使用尽可能少的字节。谢谢马丁 参考方案 Java序列化(ObjectOutputStream / ObjectInputStream)是将对象序列化为八位字节序列的一种通用方法。但…