4 ms·
What do you mean the cancel method doesn't cancel ? It does cancel with a CancellationException stored in the future and an optional interrupt send to the threa
by lenkite 7y ago
What do you mean the cancel method doesn't cancel ? It does cancel with a CancellationException stored in the future and an optional interrupt send to the thread running the blocking operation.
- mrkeen 7y ago> What do you mean the cancel method doesn't cancel? 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't
- deleted 7y ago[deleted]
- mrkeen 7y ago> optional interrupt send to the thread running the blocking operation I'm not 100% sure on what you mean by an optional interrupt, but I'm guessing it's the boolean flag on the cancel(); Let's look! public boolean cancel(boolean mayInterruptIfRunning) { boolean cancelled = this.result == null && this.internalComplete(new CompletableFuture.AltResult(new CancellationException())); this.postComplete(); return cancelled || this.isCancelled(); } Doesn't look like it's used.
- lenkite 7y agoYou are right. Even the javadoc says that `CompletableFuture` doesn't support this. Bah! will force me read the fine print next time. mayInterruptIfRunning - this value has no effect in this implementation because interrupts are not used to control processing.