This post would demo springboot and @Async examples, include:
- springboot with @Async hello world example
- springboot @Async with specific thread pool example
- springboot @Async with return value example
- SpringBoot 1.5.12
- Java 1.8
What is the @Async annotation
The @Async annotation can be provided on a method so that invocation of that method will occur asynchronously. In other words, the caller will return immediately upon invocation and the actual execution of the method will occur in a task that has been submitted to a Spring TaskExecutor.
In short words, @Async let you do the threading more easily in springboot applications. You don’t need to create a thread to execute the task anymore.
1. springboot with @Async hello world example
1.1 Enable the async with @EnableAsync on @SpringBootApplication
You must enable the async on SpringBootApplication like this:
As the following code shows, the @EnableAsync is just a class annotation with some configuration for async tasks.
1.2 Use the @Async in code
The @Async usage example code:
As the code shows, you just need to add the @Async annotation to a method,then everything is done.
1.3 Test the @Async code
and the test code is:
1.4 Run the Testcase
Run the springboot application ,and we get this result:
Here, we notice that the thread’s name is SimpleAsyncTaskExecutor, which is the default thread pool executor of the @Async task:
SimpleAsyncTaskExecutor This implementation does not reuse any threads, rather it starts up a new thread for each invocation. However, it does support a concurrency limit which will block any invocations that are over the limit until a slot has been freed up. If you are looking for true pooling, see the discussions of SimpleThreadPoolTaskExecutor and ThreadPoolTaskExecutor below.
By default when specifying @Async on a method, the executor that will be used is the one supplied to the ‘annotation-driven’ element as described above. However, the value attribute of the @Async annotation can be used when needing to indicate that an executor other than the default should be used when executing a given method.
2. springboot @Async with specific thread pool example
If we want the @Async task to run in a specified thread pool, we can define as follows:
Rerun the same testcase code, we get this:
The thread name changed according to the Executor we defined.
we can also customize the name of the Executor like this:
And run the same testcase code ,we get this:
As we can see, the name of the thread has changed to customFixedThreadPool1
3. springboot @Async with return value example
Sometimes, we want to execute a task with return value, we can use Future to achieve this:
- This async methos has a return type **@Future
**, means that this method must do a task and return a String
- This method sleep for a while and return a **AsyncResult
**, this is a holder of the future value.
The AsyncValue definition:
And the testcase is:
In this testcase, we call the async method and make sure the returned value is hi springboot async
run the code, we get the green bar and the output:
It’s so easy, do you think so? You can find the complete code on github repo
You can find detail documents about the springboot and unit testing here: