gpt4 book ai didi

java - @Async, future ,AsyncResult; "awaiting"完成异步函数有哪些方法

转载 作者:行者123 更新时间:2023-12-02 10:25:50 26 4
gpt4 key购买 nike

上下文:我对 Spring 非常陌生,以前从未使用过 @Async。我被指示根据电子邮件中发送给我的示例代码复制部分代码。

如果我要这样做,根据 @async 的文档和指南,我能知道的唯一方法是使用 Future.isDone() 并且可能有一个列表,我可以在节点“完成”时从中删除节点“并在列表为空后继续。

我想知道是否还有其他方法可以知道“ future ”何时完成?

在给我的示例代码中没有 future.isDone() 行。

他们只是在发出异步请求后调用另一个函数,该函数使用 for 循环作为

for (Entry<AClass, Future<String>> a : b.entrySet()) {

这里是否有某种神奇的迭代器巫术与“foreach”一起发生,或者我的示例代码是否有人给我不完整,或者这里还有其他事情发生?

最佳答案

我相信您对 future.isDone() 方法感到困惑

isDone 它只会返回 true 或 false

Returns true if completed in any fashion: normally, exceptionally, or via cancellation.

要对 Future 对象执行操作,您不需要调用 future.isDone() 并确定任务是否完成

公共(public) T get() 抛出InterruptedException, ExecutionException(您应该看到这个方法)

Waits if necessary for this future to complete, and then returns its result.

Throws:

CancellationException - if this future was cancelled

ExecutionException - if this future completed exceptionally

InterruptedException - if the current thread was interrupted while waiting

关于java - @Async, future ,AsyncResult; "awaiting"完成异步函数有哪些方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53964039/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com