Last active
November 26, 2017 03:40
-
-
Save babjo/937c17dc7f6dfd263294d65b91ac2c24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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