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

The Pom.xml


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:

@EnableAsync // The most import part
public class MainApp {
    public static void main(String[] args) {, args);

As the following code shows, the @EnableAsync is just a class annotation with some configuration for async tasks.

public @interface EnableAsync {

1.2 Use the @Async in code

The @Async usage example code:

public class AsyncTester {
    private static Log log = LogFactory.getLog(AsyncTester.class);

    public void asyncHelloWorld() {"hi,springboot async,"

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:

public class TestAsync {
    private AsyncTester asyncTester;

    public void testAsyncHello() {

1.4 Run the Testcase

Run the springboot application ,and we get this result:

2018-04-29 20:50:22.970  INFO 33263 --- [      cTaskExecutor-1] java8.learn.async.AsyncTester            : hi,springboot async hello,SimpleAsyncTaskExecutor-1

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:

public class ExecutorServiceConfig {
    public Executor asyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        return executor;

Rerun the same testcase code, we get this:

2018-04-29 20:50:22.970  INFO 33263 --- [      MyAsync-1] java8.learn.async.AsyncTester            : hi,springboot async hello,MyAsync-1

The thread name changed according to the Executor we defined.

we can also customize the name of the Executor like this:

public class ExecutorServiceConfig {
    //define an ExecutorService with fixed thread pool,and with a name of customFixedThreadPool
    public ExecutorService customFixedThreadPool() {
        return Executors.newFixedThreadPool(2,new CustomizableThreadFactory("customFixedThreadPool"));

//Use custom Executor named customFixedThreadPool
public void async1() {"hi,springboot async,"

And run the same testcase code ,we get this:

2018-04-29 21:04:06.772  INFO 33282 --- [ixedThreadPool1] java8.learn.async.AsyncTester            : hi,springboot async,customFixedThreadPool1

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:

public Future<String> asyncWithResult() {
    try {
        Thread.sleep(2*1000);"hi,springboot async with return value,"
        return new AsyncResult<String>("hi springboot async");
    } catch (InterruptedException e) {
    return null;


  • 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:

public class AsyncResult<V>
extends java.lang.Object
implements ListenableFuture<V>

And the testcase is:

public void testAsyncWithFuture() {
    Future<String> result = asyncTester.asyncWithResult();
    try {
        assertTrue(result.get().equals("hi springboot async"));
    } catch (Exception e) {

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:

2018-04-29 21:10:32.869  INFO 33291 --- [fixedThreadPool1] java8.learn.async.AsyncTester            : hi,springboot async with return value,customFixedThreadPool1

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: