Multithreading is good for parallel processing in any project. Through this, we can utilize the computing power of the CPU. By default, every application has a different no of threads for different purposes, we can process our own threads by using async annotation as per needs.
In Spring Boot we use an asynchronous mechanism with three quick steps.
1. Enable Async Support in Spring Boot configuration class.
Here @EnableAsync is used for enabling asynchronous processing with Java Spring Boot Configuration and switches Spring’s ability to run @Async methods.
The @Async Methods run in the background thread pool without interruption other parallel processes.
2. We can customize our async executor as per the required number of concurrent threads runs at a time.
We can configure this Bean into the Spring Boot starter configuration class.
Spring Boot uses a SimpleAsyncTaskExector to run an async method.
This Executor runs by default and it can be overridden at two levels- at the individual method levels or at the application level.
3. Add @Async Annotation to a service Method for performing thread’s task without interrupting another parallerl process.
Limitation of @Async Annotation
1. Async Annotation works on only public methods.
2. If you call async methods from within the same class then it won’t work. It means self invocation is prevented with async processing.
3. It works inconsistently if you are using autorun tools like Dev Tool in your project.