Java 8 CompletableFuture Example

A result of an expensive task:

    UUID createId() {
        return UUID.randomUUID();

needs to be passed to:

 void store(String message) {
        System.out.println("message = " + message);

...and therefore converted with:

    String convert(UUID input) {
        return input.toString();

CompletableFuture allows you to build pipeline executed asynchronously within the ForkJoinPool:

import static java.util.concurrent.CompletableFuture.supplyAsync;


...block and wait for the result:


...and even control the concurrency:

        ExecutorService es = Executors.newFixedThreadPool(2);

CompletableFuture is particularly useful in Java EE (checkout and is often used together with ManagedExecutorService /

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home:

Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition the podcast:

Stay in touch:


I don't understand why this would be useful? In the end both variants have to wait for the output, right?

So why would I want to add this complexity at all?

Posted by Bernd on October 19, 2016 at 08:33 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments