简体   繁体   English

何时使用CompletableFuture的非异步方法?

[英]When to use non-async methods of CompletableFuture?

I (mostly) understand the three execution methods of CompletableFuture : 我(大多数)理解CompletableFuture的三种执行方法:

  • non-async ( synchronous execution ) 非异步( 同步执行
  • default async (asynchronous using the default executor) default async(使用默认执行程序异步)
  • custom async (asynchronous using a custom executor) 自定义异步(使用自定义执行程序进行异步)

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: 在大多数情况下,我希望流程看起来像这样:

  1. You synchronously call an async method returning a CompletableFuture . 您同步调用返回CompletableFuture的异步方法。 It schedules some async operation to eventually provide a result. 它会调度一些异步操作以最终提供结果。 Your call returns almost immediately, with no blocking. 您的呼叫几乎立即返回,没有阻塞。
  2. Upon completion, one or more continuations may be invoked. 完成后,可以调用一个或多个延续。 Some of those may invoke additional async operations. 其中一些可能会调用其他异步操作。 Those will call into methods that will schedule additional async operations, but as before, they return almost immediately. 那些会调用方法来安排额外的异步操作,但和以前一样,它们几乎立即返回。
  3. Go to (2), or finish. 转到(2),或完成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Java CompletableFuture方法时的同步行为 - Synchronous behavior when use methods of Java CompletableFuture Apache HttpComponents 对异步和非异步请求的依赖 - Dependencies of Apache HttpComponents for async and non-async requests 在配置tomcat只使用1个线程时,使用completablefuture进行异步处理时,它使用另一个线程如何? - While configuring tomcat to use only 1 thread, when using completablefuture for async processing, it is using another thread how? 从异步方法的同步部分抛出异常,返回CompletableFuture - Throwing exceptions from sync portions of async methods returning CompletableFuture SpringBoot-2.1.3:使用 @Async 和 CompletableFuture 的并行方法调用 - SpringBoot-2.1.3: Parallel Methods Invocation with @Async with CompletableFuture Java8 - CompletableFuture - 在异步调用中按顺序运行方法 - Java8 - CompletableFuture - Running methods in async call sequentially 在 Spring Boot 的 @Async 注释中使用 CompletableFuture.supplyAsync 和 CompletableFuture.completedFuture - use of CompletableFuture.supplyAsync and CompletableFuture.completedFuture within @Async annotation in spring boot 如何有效地使用 CompletableFuture 到 map 每个输入的异步任务 - How to efficiently use CompletableFuture to map async task per input 我应该如何在 springboot async 中使用 CompletableFuture 作为 void 方法 - How should I use CompletableFuture for void method in springboot async 何时使用 CompletableFuture#thenApply(..) 而不是 thenApplyAsync(..)? - When to use CompletableFuture#thenApply(..) over thenApplyAsync(..)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM