[英]When to use non-async methods of CompletableFuture?
I (mostly) understand the three execution methods of CompletableFuture : 我(大多数)理解CompletableFuture的三种执行方法:
My question is: when should one favor the use of non-async methods? 我的问题是: 何时应该支持使用非异步方法?
What happens if you have a code block that invokes other methods that also return CompletableFuture
s? 如果你有一个代码块来调用同样返回CompletableFuture
的其他方法,会发生什么? This might look cheap on the surface, but what happens if those methods also use non-async invocation? 从表面上看,这可能看起来很便宜,但如果这些方法也使用非异步调用会发生什么? Doesn't this add up to one long non-async block that could get expensive? 这不是一个长的非异步块加起来可能会变得昂贵吗?
Should one restrict the use of non-async execution to short, well-defined code-blocks that do not invoke other methods? 是否应该将非异步执行的使用限制为不调用其他方法的简短定义的代码块?
When should one favor the use of non-async methods? 什么时候应该支持使用非异步方法?
The decision for continuations is no different than for the antecedent task itself. 延续决定与先行任务本身没有什么不同。 When do you choose to make an operation asynchronous (eg, using a CompletableFuture
) vs. writing purely synchronous code? 您何时选择使操作异步(例如,使用CompletableFuture
)而不是编写纯同步代码? The same guidance applies here. 同样的指导适用于此。
If you are simply consuming the result or using the completion signal to kick off another asynchronous operation, then that itself is a cheap operation, and there is no reason not to use the synchronous completion methods. 如果您只是使用结果或使用完成信号启动另一个异步操作,那么这本身就是一个廉价的操作,并且没有理由不使用同步完成方法。
On the other hand, if you are chaining together multiple long-running operations that would each be an async operation in their own right, then use the async completion methods. 另一方面,如果您将多个长时间运行的操作链接在一起,这些操作本身就是异步操作,那么请使用异步完成方法。
If you're somewhere in between, trust your gut, or just go with the async completion methods. 如果你介于两者之间,相信你的直觉,或者只是采用异步完成方法。 If you're not coordinating thousands of tasks, then you won't be adding a whole lot of overhead. 如果您没有协调数千个任务,那么您将不会增加大量的开销。
Should one restrict the use of non-async execution to short, well-defined code-blocks that do not invoke other methods? 是否应该将非异步执行的使用限制为不调用其他方法的简短定义的代码块?
I would use them for operations that are not long-running. 我会将它们用于不长时间运行的操作。 You don't need to restrict their use to trivially short and simple callbacks. 您不需要将它们的使用限制在简单的简短回调中。 But I think you have the right idea. 但我认为你有正确的想法。
If you're using CompletableFuture
, then you have decided that at least some operations in your code base necessitate async execution, but presumably not all operations are async. 如果您正在使用CompletableFuture
,那么您已经确定代码库中至少有一些操作需要异步执行,但可能并非所有操作都是异步的。 How did you decide which should be async and which should not? 你是如何决定哪个应该异步,哪个不应该? If you apply that same analysis to continuations, I think you'll be fine. 如果你将相同的分析应用于延续,我想你会没事的。
What happens if you have a code block that invokes other methods that also return
CompletableFuture
s? 如果你有一个代码块来调用同样返回CompletableFuture
的其他方法,会发生什么? This might look cheap on the surface, but what happens if those methods also use non-async invocation? 从表面上看,这可能看起来很便宜,但如果这些方法也使用非异步调用会发生什么? Doesn't this add up to one long non-async block that could get expensive? 这不是一个长的非异步块加起来可能会变得昂贵吗?
Returning a CompletableFuture
generally signifies that the underlying operation is scheduled to occur asynchronously, so that should not be a problem. 返回CompletableFuture
通常表示基础操作被安排为异步发生,因此不应该是一个问题。 In most cases, I would expect the flow to look something like this: 在大多数情况下,我希望流程看起来像这样:
CompletableFuture
. 您同步调用返回CompletableFuture
的异步方法。 It schedules some async operation to eventually provide a result. 它会调度一些异步操作以最终提供结果。 Your call returns almost immediately, with no blocking. 您的呼叫几乎立即返回,没有阻塞。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.