thenCombine()
is an instance method of the CompletableFuture
class, which combines the results of two completable futures.
It takes a BiFunction
as a parameter and returns a new CompletableFuture
. The BiFunction
takes the execution results of the two futures as method parameters and returns a value.
The thenCombine
method is defined in the CompletableFuture
class. The CompletableFuture
class is defined in the java.util.concurrent
package. To import the CompletableFuture
class, we use the following import statement:
import java.util.concurrent.CompletableFuture;
public <U,V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn)
CompletionStage<? extends U> other
: This is the other completable future that needs to be completed.BiFunction<? super T,? super U,? extends V> fn
: This is the BiFunction
that needs to be executed.This method returns a new CompletableFuture
.
import java.util.concurrent.*;import java.util.function.BiFunction;public class Main {static void sleep(int millis){try {Thread.sleep(millis);} catch (InterruptedException e) {e.printStackTrace();}}static void executionThread(){System.out.println("Thread execution - " + Thread.currentThread().getName());}public static void main(String[] args){CompletableFuture<String> completableFuture1 = CompletableFuture.supplyAsync(() -> {sleep(1000);String stringToPrint = "Educative";System.out.println("----\nsupplyAsync first future - " + stringToPrint);executionThread();return stringToPrint;});CompletableFuture<String> completableFuture2 = CompletableFuture.supplyAsync(() -> {sleep(2000);String stringToPrint = "Edpresso";System.out.println("----\nsupplyAsync second future - " + stringToPrint);executionThread();return stringToPrint;});BiFunction<String, String, String> stringStringBiFunction = (res1, res2) -> String.format("\"completableFuture1 result - %s | completableFuture2 result - %s\"", res1, res2);CompletableFuture<String> finalCompletableFuture = completableFuture1.thenCombine(completableFuture2, stringStringBiFunction);System.out.println("Output of thenCombine - " + finalCompletableFuture.join());sleep(3000);}}
Lines 1–2: We import the relevant packages and classes.
Lines 6–12: We define a function called sleep()
, which makes the current thread sleep for the given amount of time (in milliseconds).
Lines 14–16: We define a function called executionThread()
, which prints the current thread of execution.
Lines 20–26: We create a completable future called completableFuture1
, using the supplyAsyc()
method. We pass a supplier to it that sleeps for one second and then invokes the executionThread()
. Finally, this method returns a string value.
Lines 28–34: We create another completable future called completableFuture2
, using the supplyAsyc()
method. We pass a supplier to it that sleeps for two seconds and then invokes the executionThread()
. Finally, this method returns a string value.
Line 36: We create a BiFunction
called stringStringBiFunction
, which returns the results of both the futures as a string.
Line 37: We invoke the thenCombine
method on completableFuture1
. We pass completableFuture2
and stringStringBiFunction
as method arguments to the thenCombine
method. This returns a new CompletableFuture
called finalCompletableFuture
.
Line 38: We print the value of finalCompletableFuture
to the console.
Line 39: The main thread is made to sleep for three seconds.