简体   繁体   English

Spring Boot Async 方法如何使用 ThreadPool 处理请求

[英]How Spring Boot Async method handles request using ThreadPool

I am not able to understand how Async works.I know that webserver has its own thread pool to handle multiple requests.我无法理解异步是如何工作的。我知道网络服务器有自己的线程池来处理多个请求。 When we implement asynchronous web service with spring boot and configure let's say "n" threads using TaskExecutor , does that mean n threads created for each thread coming as request?当我们使用 spring 启动和配置异步 web 服务时,假设使用TaskExecutor配置“n”个线程,这是否意味着为每个作为请求的线程创建的n 个线程?

To clarify, the server will assign a request to a thread from its ThreadPool.为了澄清,服务器将从其 ThreadPool 中将请求分配给线程。 So when this thread starts execution and calls a function marked with @Asynch , it can create another "n" threads for each incoming thread to handle async work.因此,当该线程开始执行并调用标有@Asynch的 function 时,它可以为每个传入线程创建另一个“n”个线程来处理异步工作。 Please let me know if my understanding is correct.请让我知道我的理解是否正确。

If What I understand is correct then how to decide what amount of pool size should be created?如果我的理解是正确的,那么如何决定应该创建多少池大小? Can someone please give some example.有人可以举一些例子。

There are 2 things, one is a tomcat thread pool from where each request takes a thread and is being processed (this is configured in server.tomcat.max-threads) and a separate thread pool that is used for running @Aync tasks.有两件事,一是 tomcat 线程池,每个请求都从其中获取一个线程并正在处理(这在 server.tomcat.max-threads 中配置)和一个单独的线程池,用于运行 @Aync 任务。 When you run a method (task actually) annotated as async, this will create a task and wait in the async queue.当您运行注释为异步的方法(实际上是任务)时,这将创建一个任务并在异步队列中等待。 The initial request won't be blocked and if you don't wait for the async to finish will return and continue doing his job.初始请求不会被阻止,如果您不等待异步完成将返回并继续执行他的工作。 Simply put, annotating a method with @Async will make it execute in a separate thread and the caller will not wait for the completion.简单地说,使用@Async 注释一个方法将使它在一个单独的线程中执行,调用者不会等待完成。

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

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