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

runAsync() – running a task asynchronously

When we want to execute a background activity task asynchronously and do not want to return anything from that task, we can use the CompletableFuture.runAsync() method. It takes a parameter as a Runnable object and returns the CompletableFuture<Void> type.

Let's try to use the runAsync() method by creating another controller method in our BankController class, with the following example:

@RequestMapping(value = "/synccust")  @ResponseBody  public CompletableFuture<String> syncCustomerDetails() {    LOGGER.info("Entering in controller");    CompletableFuture<String> completableFuture = new     CompletableFuture<>();    CompletableFuture.runAsync(new Runnable() {            @Override      public void run() {        try {            completableFuture.complete(syncService.syncCustomerAccount() ...

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