Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
private static void demoCancel() throws InterruptedException, ExecutionException {
CompletableFuture<String> completableFutureFirst = CompletableFuture.supplyAsync(() -> {
System.out.println("Task Running inside completable Future");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "result";
});
completableFutureFirst.cancel(true);
boolean isCancelled = completableFutureFirst.isCancelled();
System.out.println("Completable Future is cancelled :: "+isCancelled);
String result = completableFutureFirst.get();
System.out.println("Result :: "+result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment