Java SE API
CompletableFuture.thenCombine()
Combine two successful input values.
Formal Syntax & Structure
java syntaxjava
<U,V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| other | CompletionStage<? extends U> | Other input stage |
| fn | BiFunction<? super T, ? super U, ? extends V> | Combine both successful values |
Working Example
Example Snippetjava
import java.util.concurrent.*;import java.util.concurrent.atomic.*;import java.util.concurrent.locks.*;public class Main {
public static void main(String[] args) throws Exception { CompletableFuture<Integer> price = new CompletableFuture<>(); CompletableFuture<Integer> shipping = new CompletableFuture<>(); CompletableFuture<Integer> total = price.thenCombine(shipping, Integer::sum); shipping.complete(15); System.out.println(total.isDone()); price.complete(80); System.out.println(total.join()); }}