Java 9’s CompletableFuture introduces several new methods amongst which are orTimeout and completeOnTimeOut that provide built-in support for dealing with timeouts.
The method orTimeout has the following signature:
1 2
public CompletableFuture<T> orTimeout(long timeout, TimeUnit unit); public CompletableFuture<T> completeOnTimeout(T value, long timeout, TimeUnit unit);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
CompletableFuture.supplyAsync(() -> findBestPrice("LDN - NYC"), executorService) .thenCombine(CompletableFuture.supplyAsync(() -> queryExchangeRateFor("GBP")), this::convert) .orTimeout(1, TimeUnit.SECONDS) .whenComplete((amount, error) -> { if (error == null) { System.out.println("The price is: " + amount + "GBP"); } else { System.out.println("Sorry, we could not return you a result"); } });