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?
allOf() is a static method of the CompletableFuture class. It returns a new CompletableFuture object when all of the specified CompletableFutures are complete.
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.
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.
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.
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 CompletableFuture
s 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With