Home > Back-end >  CompletableFuture join vs CompletableFuture thenApply
CompletableFuture join vs CompletableFuture thenApply

Time:10-28

I have this two snippets in a Spring WebMVC controller:

First: thenApplyAsync

@RequestMapping(value = {"/index-standard"}, method = RequestMethod.GET)
    public CompletableFuture<String> indexStandard(Model model) {
        return projectService
                .findMine(getCurrentApplicationUser())
                .thenApplyAsync((result) -> {
                    model.addAttribute("projects", result);
                    return "/projectmanagement/my/index";
                });
    }

Second: join()

@RequestMapping(value = {"/index-nonstandard"}, method = RequestMethod.GET)
    public String indexNonStandard(Model model) {
        CompletableFuture<Iterable<ProjectDto>> mine = projectService.findMine(getCurrentApplicationUser());
        CompletableFuture.allOf(mine).join();
        model.addAttribute("projects", mine.join());
        return "/projectmanagement/my/index";
    }

For my understanding:

First option:

  • join() waits for execution of the CompletableFuture code.
  • the code is executed on a dedicated thread.
  • the http thread from the origin http request is available for further requests while the code executes
  • at the end (when the code is finished) the view will be rendered

Second option:

  • the same as the first option?

What is the difference in these options?

So is there a preferable solution (first or second)?

Both options work.

CodePudding user response:

.join() will block the execution of the thread and wait until the result is ready. Usually, that's not the behaviour one wants when writing async applications.

  • Related