Try it.
@Test
public void cannotCancelARunningFuture() {
// Future that just sleeps
CompletableFuture<Void> sleeping =
CompletableFuture.supplyAsync(() -> {
int i = 0;
while (true) {
System.out.println("zzzz: " + i++);
threadSleep(300);
}
});
// Make sure it started
threadSleep(1000);
sleeping.cancel(true);
System.out.println("Sleep was cancelled");
threadSleep(1000);
System.out.println("Haha no it wasn't");
}
=============================== zzzz: 0
zzzz: 1
zzzz: 2
zzzz: 3
Sleep was cancelled
zzzz: 4
zzzz: 5
zzzz: 6
Haha no it wasn'tNo comments yet.