Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CompletableFuture allOf method behavior

I have the following piece of java code:

CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3);
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
            return "Result of Future 1";
        });

        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3);
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
            return "Result of Future 2";
        });

        CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3);
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
            return "Result of Future 3";
        });

        boolean isdone = CompletableFuture.allOf(future1, future2, future3).isDone();

        if (isdone) {
            System.out.println("Future result " + future1.get() + " | " + future2.get() + " | " + future3.get());
        } else {
            System.out.println("Futures are not ready");
        }

When I run this code it always prints "Futures are not ready". I am using allOf method here which should wait for all the futures to get completed, but the main thread is not waiting here and priniting the else part. Can someone please help me understand what is going wrong here?

like image 927
Abhilash Avatar asked Nov 20 '18 10:11

Abhilash


People also ask

What is CompletableFuture allOf?

allOf() is a static method of the CompletableFuture class. It returns a new CompletableFuture object when all of the specified CompletableFutures are complete.

What is defaultExecutor method of CompletableFuture interface?

defaultExecutor() − Returns a new incomplete CompletableFuture of the type to be returned by a CompletionStage method. Subclasses of CompletableFuture class should override this method to return an instance of the same class as this CompletableFuture.

How Completable future is non-blocking?

CompletableFuture is used for asynchronous programming in Java. Asynchronous programming is a means of writing non-blocking code by running a task on a separate thread than the main application thread and notifying the main thread about its progress, completion or failure.

Is CompletableFuture asynchronous?

What is CompletableFuture? A CompltableFuture is used for asynchronous programming. Asynchronous programming means writing non-blocking code. It runs a task on a separate thread than the main application thread and notifies the main thread about its progress, completion or failure.


1 Answers

I am using allOf method here which should wait for all the futures to get completed

That's not what allOf does. It creates a new CompletableFuture that is completed when all of the given CompletableFutures complete. It does not, however, wait for that new CompletableFuture to complete.

This means that you should call some method that waits for this CompletableFuture to be completed, at which point all the given CompletableFutures are guaranteed to be complete.

For example:

CompletableFuture<Void> allof = CompletableFuture.allOf(future1, future2, future3);
allof.get();

if (allof.isDone ()) {
    System.out.println("Future result " + future1.get() + " | " + future2.get() + " | " + future3.get());
} else {
    System.out.println("Futures are not ready");
}

Output:

Future result Result of Future 1 | Result of Future 2 | Result of Future 3
like image 74
Eran Avatar answered Oct 22 '22 10:10

Eran