Skip to content

Instantly share code, notes, and snippets.

@babjo
Last active November 26, 2017 03:40
Show Gist options
  • Save babjo/937c17dc7f6dfd263294d65b91ac2c24 to your computer and use it in GitHub Desktop.
Save babjo/937c17dc7f6dfd263294d65b91ac2c24 to your computer and use it in GitHub Desktop.
public Future<String> calculateAsyncWithCancellation() throws InterruptedException {
CompletableFuture<String> completableFuture = new CompletableFuture<>();
Executors.newCachedThreadPool().submit(() -> {
Thread.sleep(500);
completableFuture.cancel(false);
return null;
});
return completableFuture;
}
Future<String> future = calculateAsyncWithCancellation();
future.get(); // CancellationException
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment