CompletableFuture——快速失败的聚合未来

2023-11-22

我一直在使用CompletableFuture.allOf(...)帮助器创建聚合期货,只有当它们的组合期货被标记为完成时才会“完成”,即:

CompletableFuture<?> future1 = new CompletableFuture<>();
CompletableFuture<?> future2 = new CompletableFuture<>();
CompletableFuture<?> future3 = new CompletableFuture<>();

CompletableFuture<?> future = CompletableFuture.allOf(future1, future2, future3);

我希望此功能略有不同,在以下情况下,总体未来是完整的市场:

  • 所有期货均已成功完成OR
  • 任何一个未来都已失败完成

在后一种情况下,聚合 future 应该(例外地)立即完成,而不必等待其他 future 完成,即快速失败.

为了说明这一点,与CompletableFuture.allOf(...)考虑一下:

// First future completed, gotta wait for the rest of them...
future1.complete(null);
System.out.println("Future1 Complete, aggregate status: " + future.isDone());

// Second feature was erroneous! I'd like the aggregate to now be completed with failure
future2.completeExceptionally(new Exception());
System.out.println("Future2 Complete, aggregate status: " + future.isDone());

// Finally complete the third future, that will mark the aggregate as done
future3.complete(null);
System.out.println("Future3 Complete, aggregate status: " + future.isDone());

Using allOf(...),这段代码产生:

Future1 Complete, aggregate status: false
Future2 Complete, aggregate status: false
Future3 Complete, aggregate status: true

而我的替代聚合实现将在功能 2 完成后返回“true”,因为它是一个例外。


我在 Java 标准库中找不到任何可以帮助我实现这一目标的实用程序,这感觉很奇怪……因为它是一个相对普通的用例。

看看实施情况CompletableFuture.allOf(...)很明显,这些场景背后的逻辑相当复杂。我不想自己写这个,我想知道是否有其他选择?


虽然语法上不如CompletableFuture.allOf(...)方法,看来thenCompose(...)可以提供解决方案:

CompletableFuture<?> future = future1.thenCompose((f) -> future2).thenCompose((f) -> future3);

这将产生所需的:

Future1 Complete, aggregate status: false
Future2 Complete, aggregate status: true
Future3 Complete, aggregate status: true

这可以包含在一个辅助方法中,该方法将为调用者提供一些语法细节:

private static CompletableFuture<?> composed(CompletableFuture<?> ... futures) {

    // Complete when ALL the underlying futures are completed
    CompletableFuture<?> allComplete = CompletableFuture.allOf(futures);

    // Complete when ANY of the underlying futures are exceptional
    CompletableFuture<?> anyException = new CompletableFuture<>();
    for (CompletableFuture<?> completableFuture : futures) {
        completableFuture.exceptionally((t) -> {
            anyException.completeExceptionally(t);
            return null;
        });
    }

    // Complete when either of the above are satisfied
    return CompletableFuture.anyOf(allComplete, anyException);
}

允许:

CompletableFuture<?> future = composed(future1, future2, future3);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

CompletableFuture——快速失败的聚合未来 的相关文章

随机推荐

  • 如何检测QWidget的关闭按钮被按下?

    我创建一个新的QWidget对象 我想知道何时按下关闭按钮 我尝试过以下代码 pWindow new QWidget connect pWindow SIGNAL triggered this SLOT processCloseButton
  • 如何在 Tensorflow Serving 中进行批处理?

    部署 Tensorflow Serving 并运行 Inception V3 测试 工作正常 现在 想要为 Inception V3 进行批处理 例如 希望发送 10 张图像 而不是一张 进行预测 怎么做 要更新哪些文件 inception
  • hadoop map减少二次排序

    谁能解释一下 hadoop 中的二次排序是如何工作的 为什么一定要使用GroupingComparator它在 hadoop 中是如何工作的 我正在浏览下面给出的链接 并对 groupcomapator 的工作原理产生疑问 谁能解释一下分组
  • java.sql.SQLException: ORA-00928: 缺少 SELECT 关键字。使用 JDBC 将记录插入数据库时

    当我尝试将一些行插入数据库时 出现错误 所以这是代码 try String insertStmt INSERT into MY TABLE RECORD TYPE FILE TYPE DATE BATCH NO RECORD COUNT V
  • 新的 C# 异步功能是否在编译器中严格实现

    作为一名对探索 事物如何工作 感兴趣的 C 程序员 我有兴趣更多地了解创造新事物的过程async特色工作 我关注了 Eric Lippert 关于异步的优秀文章系列 异步博客文章 我不记得在任何地方看到过任何关于此功能的实现 在高级别上 的
  • 动态改变CSS

    我需要确定哪个CSS应该应用于某些特定页面 我有一个包含子项和内容的母版页默认 aspx and 服务 aspx是母版页的子级 我想要的是当用户导航时默认 aspx or 服务 aspx 系统应适用默认CSS文件否则我想应用一些普通CSS
  • Apache 身份验证:失败时重定向,可靠吗?

    我已将 ErrorDocument 401 设置为指向我网站的帐户创建页面 但并非所有浏览器似乎都支持此重定向 Safari 此外 其他浏览器 Firefox Chrome 永远不会停止询问密码并显示 ErrorDocument 这导致大量
  • .lite 和 .tflite 格式之间有什么区别

    两者有什么区别 lite和 tflite格式 TensorFlow 格式 如果没有区别 为什么会有两个 除了问题之外 我似乎无法使用以下命令上传我的模型 liteFirebase ML 套件的扩展文件 原因可能是什么 ML 开发人员首先训练
  • 自动完成用户输入 PowerShell 2.0

    我有一个很大的数据列表 超过 1000 个不同的值 我希望用户能够从 PowerShell 控制台的列表中选择某些值 在控制台中允许用户快速选择值的最简单方法是什么 我想做一些诸如制表符补全或使用箭头键滚动值的功能 但我不确定如何执行这些操
  • Qt嵌入编译错误。修复“错误:没有这样的指令”错误

    我正在尝试使用安装在 home user Software 的 beagle 板的 angstrom 工具链来编译 qt 4 7 4 我收到的错误是 corelib arch qatomic arm h 131 错误 没有这样的指令 swp
  • jQuery 加载函数

    我使用以下脚本来调用 onload 函数 但它在 IE 中不起作用 body attr onload calFact 如果您使用 jQuery 您可以使用ready 函数如下 function callFact 或者更简单 只需将方法传递给
  • 检查 JSON 中是否存在子对象

    我正在使用以下命令检查 JSON 字符串中的对象是否存在 JSONObject json null try json new JSONObject myJsonString catch JSONException e e printStac
  • 与 Java 11 兼容的最低 Spring 版本

    我需要很快将应用程序升级到 Java 11 我想知道与 Java 11 兼容的最低 Spring 版本是多少 我目前正在使用 Java 8 和 Spring 4 2 7 从他们的任务跟踪器 SPR 16391 与 JDK 11 的兼容性 J
  • 如何 grep 查找文件中的 URL?

    例如 我有一个巨大的 HTML 文件 其中包含 img URL http ex example com hIh39j ud9wr4 Uusfh jpeg 我想获取这个 URL 假设它是only整个文件中的 url cat file html
  • 从 Windows 命令提示符交互式运行 R [关闭]

    Closed 这个问题是无关 目前不接受答案 我需要从 win 中的命令提示符交互式启动 R 以便能够显示绘图 知道我该怎么做吗 谢谢 输入适合您系统的正确版本 C path to R bin R 该完整路径可以从 R 的安装位置获取 或者
  • Vim - 激活 PHP 文件上的 HTML 片段

    我正在使用vim和snipMate 很多时候我需要将HTML文件命名为PHP 只是因为一两行代码 我每次创建 PHP 文件时 vim 都会将其视为 PHP 文件 因此 HTML 片段不可用 因此必须使用命令手动激活 HTML 片段 set
  • 在网站上创建网站图标的最佳实践是什么? [关闭]

    Closed 这个问题是基于意见的 目前不接受答案 Question 创建一个的最佳实践是什么favicon在网站上 并且是一个 ico同时包含 16x16 和 32x32 图像的文件比 png文件只有 16x16 今天首选的正确方法是否无
  • 实现Spring Data存储库的自定义方法并通过REST公开它们

    我正在尝试将自定义方法添加到我的 Spring 数据存储库PersonRepository如中所述1 3 Spring Data 存储库的自定义实现并通过 REST 公开这些方法 初始代码来自使用 REST 访问 JPA 数据示例 这里是添
  • com.sun.istack.SAXException2 :实例...正在替换“java.lang.Object”,但是...绑定到匿名类型

    我正在将项目从版本 1 x 升级到 jaxb 2 2 7 我的应用程序有时可以运行 但在一些回复中我看到了这一点 java lang RuntimeException javax xml bind MarshalException with
  • CompletableFuture——快速失败的聚合未来

    我一直在使用CompletableFuture allOf 帮助器创建聚合期货 只有当它们的组合期货被标记为完成时才会 完成 即 CompletableFuture future2 new CompletableFuture lt gt C