O'Reilly logo

Hands-On High Performance with Spring 5 by Dinesh Radadiya, Prashant Goswami, Pritesh Shah, Subhash Shah, Chintan Mehta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The @Async annotation

Once asynchronous processing is enabled, then the methods that are annotated with the @Async annotation will execute asynchronously.

The following is a simple example of the @Async annotation:

public class AsyncTask {  private static final Logger LOGGER =   Logger.getLogger(AsyncTask.class);  @Async  public void doAsyncTask() {    try {      LOGGER.info("Running Async task thread : " +       Thread.currentThread().getName());    } catch (Exception e) {    }  }}

We can also annotate the @Async annotation to a method with the return type, as follows:

@Async  public Future<String> doAsyncTaskWithReturnType() {    try     {      return new AsyncResult<String>("Running Async task thread : " +       Thread.currentThread().getName());    }     catch (Exception e) {     }

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required