How Spring Boot Async method handles request using ThreadPool

Viewed 862

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?

To clarify, the server will assign a request to a thread from its 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. 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.

1 Answers

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. 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.

Related