Skip to content

Instantly share code, notes, and snippets.

@theboreddev
Created June 30, 2020 21:27
Show Gist options
  • Save theboreddev/7db762d0c85cc731941a424784bb4cc7 to your computer and use it in GitHub Desktop.
Save theboreddev/7db762d0c85cc731941a424784bb4cc7 to your computer and use it in GitHub Desktop.
timeout
CompletableFuture<String> subTask = new CompletableFuture<>();
CompletableFuture<Result> chain = CompletableFuture.runAsync(() -> System.out.println("Start"))
.thenCombine(subTask, (nil, text) -> {
if (text == null || text.isEmpty())
throw new IllegalArgumentException("Text cannot be null or empty!");
return Result.COMPLETED;
})
.completeOnTimeout(Result.TIMEOUT, 500, TimeUnit.MILLISECONDS)
.exceptionally(exception -> Result.FAILED);
final Result result = chain.get();
System.out.println("Result is : " + result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment