cost 114 ms
从异步方法返回 object 到 CompletableFuture<object> 列表?<div id="text_translate"><p> 我有一个要使用异步方法处理的对象列表(来自 model class 名称)。 异步方法将获取列表中的每个名称 object 并对其进行处理(检查它是否未被占用),我希望在所有名称都被异步方法处理后返回到isNameAvailable(LinkedHashSet<Name> nameList)方法</p><p>我的执行者配置</p><pre>@Configuration @ManagedResource public class ThreadConfig { @Bean(name = "nameAvailabilityExecutor") public Executor nameAvailabilityExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(100); executor.setMaxPoolSize(1200); executor.setQueueCapacity(10000); executor.setThreadNamePrefix("nameAvailabilityExecutor-"); executor.initialize(); return executor; } }</pre><p> 服务 class 将调用另一个 class 中的异步方法</p><pre>public LinkedHashSet<Name> isNameAvailable(LinkedHashSet<Name> nameList) { LinkedHashSet<Name> nameCheckedList = new LinkedHashSet<>(); for (Name nameObj: nameList) { Name nameCheckedObj = domainAvailabilityServiceThread.isNameAvailable(nameObj); nameCheckedList.add(nameCheckedObj); } return nameCheckedList; }</pre><p> 将进行处理的异步方法</p><pre>@Async("nameAvailabilityExecutor") public Name isNameAvailable(Name nameObj) { String name = nameObj.getName(); if (getByNameCheck(name)) { nameObj.setAvailable(true); } else { nameObj.setAvailable(false); } return nameObj; }</pre><p> 根据我的理解 CompletableFuture 是我需要在这里使用的吗? 在这种情况下使用 CompletableFuture 的正确方法是什么?</p></div></object> - Return object from async method into CompletableFuture<Object> list?

我有一个要使用异步方法处理的对象列表(来自 model class 名称)。 异步方法将获取列表中的每个名称 object 并对其进行处理(检查它是否未被占用),我希望在所有名称都被异步方法处理后返回到isNameAvailable(LinkedHashSet&lt;Name&gt; nameLi ...

当数据集或负载增加时,CompleteableFuture 需要更长的时间 - CompleteableFuture takes longer time when the dataset or load increases

我有一个要求,我需要为 100 个用户调用 2 个 api。 第一个 API 是一个 88142662888 api 来生成令牌,这需要 2 秒,第二个调用是使用生成的令牌实际 API,大约需要 8-9 秒。 所以总的来说,为一个用户处理两个 api 需要大约 10 秒。 我使用 Completea ...

CompletableFuture 不调用其他 Bean 中的 static 方法 - CompletableFuture not calling static methods in other Beans

这是我的第一篇文章,如果格式不正确或提供的信息不正确,请原谅我,但我已经搜索了好几天的答案,但仍然无法弄清楚。 我有一个测试,我正在使用 Spring 注入 bean。 我需要同时运行多个 API 调用,所以我使用 CompletableFuture 来完成它。 但是当我这样做时,似乎在其他组件中调 ...

如何为使用 Supplier 构建的重试机制编写 JUnit5 或 Mockito 测试<completablefuture>作为方法参数? </completablefuture> - How to write JUnit5 or Mockito test for a retry mechanism built using Supplier<CompletableFuture> as a method param?

赏金将在 7 天后到期。 此问题的答案有资格获得+250声望赏金。 PacificNW_Lover正在寻找可靠来源的答案。 有一个不是特定于服务器的通用框架,我们用它来导入和重用我们微服务中的对象,但可以用于支持 Java 的任何特定 Java 程序 8. 负责创建一个retry()机制,该机制 ...

在 CompletableFuture 中返回 autocloseable object 并在 whenComplete 中使用它 - Return autocloseable object inside a CompletableFuture and use it in whenComplete

我想在 CompletableFuture 中返回一个可自动关闭的 object 并在 whenComplete 中使用它,而无需稍后手动关闭它。 这是我试过的代码,但当然它不会工作,因为当我返回 Jedis object 时它会自动关闭。我没有任何其他想法。 ...

使用注释“@EventListener(ApplicationReadyEvent.class)”调用的方法按预期工作,但直接调用失败 - Method called using annotation '@EventListener(ApplicationReadyEvent.class)' working as expected, but direct call falls

当@EventListener(ApplicationReadyEvent.class)调用该方法时,预期的行为最终会发生。 但是当发生直接方法调用时,CompletableFuture 中的循环只重复一次 asyncThreadPool池也从configproperty.getPipel.net ...

Java 多线程中的 Thread vs Runnable vs CompletableFuture - Thread vs Runnable vs CompletableFuture in Java multi threading

我正在尝试在我的 Spring Boot 应用程序中实现多线程。 我只是 Java 中多线程的初学者,在进行了一些搜索并阅读了各个页面上的文章之后,我需要澄清以下几点。 所以; 1.据我所知,我可以使用Thread 、 Runnable或CompletableFuture来在 Java 应用程序 ...

CompletableFuture: anyOne 调用了哪个方法,我怎样才能得到想要的值 - CompletableFuture: anyOne which method is called and how can I get the desirable value

我需要异步调用一个带有不同输入参数(比如id)的方法,该方法返回true或false,而false output是不可取的,所以我应该等待一个true output。一旦我得到一个true,我不关心其他电话,我只需要知道真正的 output 对应于哪个输入(id)。 我调用这段代码,我应该如何知道r ...

如何基于Java CompletableFuture构造一个异步执行的任务链 - How to Construct an asynchronously executed task chain based on Java CompletableFuture

我有N个任务要执行,任务个数不固定。 只有在上一个任务完成后才能执行下一个任务。 整个任务链如何异步执行? 如果任务数是固定的,比如N=2,我可以用下面的代码。 N不固定怎么办 我查看了 Compeltable 的 API 文档,但似乎没有一个能解决我的问题。 我尝试用循环来控制任务提交,但是都失 ...

我们是否有替代 Java 9 CompletableFuture 方法来处理超时? - Do we have alternative to Java 9 CompletableFuture methods to handle timeouts?

我想要一种方法来处理可完成期货的超时(自定义值),如果有超时,我们可以为未来对象分配默认值。 我知道在 Java 9 及更高版本中有几种方法可以处理它。 但是有没有类似于 Java 9 CompletableFuture 方法的替代方法 ...

invokeAll 除了 CompletableFuture - invokeAll but CompletableFuture

我的任务是使用 ExecutiveService 和 CompletableFuture 异步加载文件,并测量 2、4、8 个线程且没有并行化的执行时间这是我的第一个方法: 它工作得很好但是这样,我不使用CompletableFuture,我试过这个: 但是我得到的线程数几乎相同,虽然我有 31 ...

CompletableFuture.get() 输出而不是状态而不是响应主体 - CompletableFuture.get() outputs rather status than response body

我有以下场景:我设置了一个客户端,它向服务器发送一个异步的 HTTP reuqest。 客户端收到一个 CompletableFuture。 到目前为止一切正常。 但是,我无法访问我发送的请求的响应。 而是包含请求的状态。 更详细地说,我得到 我怎样才能得到实际的结果? 这是我的代码... Res ...

Java 顺序代码的 CompletableFuture - Java CompletableFuture for sequential code

我的新团队正在编写一个 Java gRPC 服务,为了确保我们永远不会阻塞请求线程,我们最终将或多或少的所有方法包装在CompletableFuture中,即使这些端点在概念上是顺序操作列表(无并行性)。 所以代码看起来像(如果需要,最后可以使用 Java 示例): 语境: 实际上,应用程序要大得 ...

将运行任务的 Class 传递给 Completable Future SupplyAsync - Passing Class that runs task to Completable Future SupplyAsync

尝试比较期货与已完成期货的实施,并检查可完成期货的非阻塞性质是否是解决我的问题的更好用例。 让我的可完成实现正常工作时遇到问题。 Completable 实现的问题在于,它无法识别 GetSiteMeterTariffValues 在任务完成后返回不同的类型。 它返回 但是任务执行者对此很好 所 ...


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