直接上代码
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
public class Test3 {
public static void main(String[] args) {
CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 2;
}).thenCombine(CompletableFuture.supplyAsync(() -> {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 2;
}), (t1, t2) -> {
return t1 + t2;
});
System.out.println(integerCompletableFuture.join());
}
}
// 4