Posts

Showing posts with the label multithreading

How to do @Async operation in spring boot

Using @Async annotation we can create the asynchronous method in spring. If we add @Async annotation on the method then spring Will execute the method in a separate thread in the background thread pool.  Enabling @Async support in spring boot To enable asynchronous processing, you need to add @EnableAsync in the spring configuration class. @EnableAsync annotation will look for a method marked with @Async annotation and run them in the background thread pool.  @Configuration @EnableAsync public class AsyncConfig { ... }   @Async has two rules It must be applied to the public method only. Self-invocation is not allowed. That means calling the async method from the same class is not work. The reason for the above rule is very simple The method needs to be public so that can be provided and self-innovation doesn't work because it passes the proxy and call the method directly. The method with void return type The method with a void return type works like a runnable thre...