'How to optimize async threads in spring boot
I have Spring Boot app running in embedded tomcat. There are around 50 concurrent HTTP sessions and each of them is served by 5-7 concurrently running async backend calls (@Async). There is no specific threads configuration for Tomcat or Spring Boot.
I found that long running thread (does not not matter whether it is Tomcat or async call) seriously decreases performance of other. For example, if I generate report using CR JRC which takes 20-40 seconds, most of async threads look paralyzed.
How can I optimize the code and configuration to resolve the performance issue?
Solution 1:[1]
From your description, there could be several bottlenecks in your configuration. But one could be the number of threads available in your system. The best you could do from here is profile your application and check what threads are available, how they are used, and where do they block.
Furthermore, assuming the number of threads is the issue, then when you say
There is no specific threads configuration for Tomcat or Spring Boot.
if it means you are running on the default ThreadPoolExecutor, then you should check the documentation and default values on how to configure your thread pool, and scale accordingly.
The @Async annotation also allows you to specify which bean Executor to use.
// use default Executor
@Async
public void asyncMethodUsingDefaultExecutor() {}
// use of Executor with qualifier specificExecutorBeanQualifier
@Async("specificExecutorBeanQualifier")
public void asyncMethodUsingSpecificExecutor() {}
You could use this to have a separated Threadpool to handle long-running tasks and another one for the others.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Baptiste Beauvais |
