Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @Async vs completable future run async

I might have been doing something really wrong. So, I have Spring's @Async

Let's say I have this piece of code

@Async("poolbeanname") 
Function () {
     // some code 
}

I have one more, let's say I have this piece of code

@Async("poolbeanname") 
Function () {
     CompletableFuture.runAsync{ new Runnable ()...} 
} 

Now with the second code I can see, some threads got spawned but the first approach doesn't seem to spawn more than one?

like image 756
Tilak Raj Avatar asked Nov 08 '25 20:11

Tilak Raj


1 Answers

To enable using @Async you should use @EnableAsync

Let’s start by enabling asynchronous processing with Java configuration – by simply adding the @EnableAsync to a configuration class:

@Configuration
@EnableAsync
public class SpringAsyncConfig { ... }

And you must use a public method called from other class:

@Async has two limitations:

  • it must be applied to public methods only

  • self-invocation – calling the async method from within the same class – won’t work

like image 138
user7294900 Avatar answered Nov 10 '25 10:11

user7294900



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!