I'm learning about threads and how threads work when building a web application.
As per my understanding the flow is as follows:
- User sends Http request
- Tomcat creates a thread running the controller.
- Spring boot runs an async annotated method, that runs code on a seperate thread pool created by the spring boot app.
- The tomcat thread is released until the async method is completed to handle more requests.
Am I correct in my understanding?
Does spring boot create its own thread pool to run async operations on freeing the main tomcat thread?
CodePudding user response:
When the asynchronous method is called, the tomcat thread isn't "released" or "freed". It proceeds with the next instruction after the async method call and keeps on going (unless it does something like call get on a future returned by the async method so that it blocks until the future completes). Both threads execute concurrently.
It is true that Spring has its own separate threadpool that it uses for async methods. The Executor used is configurable, @Async takes the name of the executor as an argument so different methods can use different pools if needed. And Tomcat has a pool for its threads, of course.