1

in Java 8, the param mayInterruptIfRunning of method CompletableFuture#cancel(mayInterruptIfRunning) not take effect, which I think violate contract defined in java.util.concurrent.Future.

It will cause problem when migrate code from Future to CompletableFuture without noticing this contract change.

Also it seems violate Liskov substitution principle.

related article: https://www.nurkiewicz.com/2015/03/completablefuture-cant-be-interrupted.html

Jason
  • 521
  • 2
  • 7
  • Link is broken. – Antoniossss Apr 03 '19 at 08:40
  • How do you test that? `may` is not `must`. – Antoniossss Apr 03 '19 at 08:41
  • 1
    once a thread is started it cannot be interuppted. Upstream nothing can be done – Tilak Raj Apr 03 '19 at 08:41
  • @BOTJr. eeerrrr [`Thread.interrupt`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Thread.html#interrupt())?? – Boris the Spider Apr 03 '19 at 08:43
  • mayInterruptIfRunning is the param name, not doc, please read the doc in Future. @Antoniossss – Jason Apr 03 '19 at 08:43
  • But the task must handle interruption it first place. I checked the docs prior to my comment, and that was the conclusion. – Antoniossss Apr 03 '19 at 08:44
  • @BOTJr. would you please give more detail? – Jason Apr 03 '19 at 08:44
  • https://stackoverflow.com/a/23329340/5871514 – Tilak Raj Apr 03 '19 at 08:45
  • Possible duplicate of [How to cancel Java 8 completable future?](https://stackoverflow.com/questions/23320407/how-to-cancel-java-8-completable-future) – Tilak Raj Apr 03 '19 at 08:49
  • @BOTJr. the link does not answer the question, interruption can use in non-blocking , and interruption status is widely used by this way:`while(!Thread.interrupted){do non-blocking things}` – Jason Apr 03 '19 at 08:50
  • The documentation of `CompletableFuture#cancel(boolean)` tells you the why: "_this value has no effect in this implementation **because interrupts are not used to control processing**_". This change in behavior may violate the contract of `Future` and/or some principles, but that's the reason they give. Implementation wise, the `CompletableFuture` does not maintain a reference to the executing thread and thus can't interrupt it. – Slaw Apr 03 '19 at 09:00
  • 1
    @Slaw @BOTJr I see the doc in `CompletableFuture`, it means `cancel` can only effect on downstream. But it can not take effect on stage which is currently running, which I think is a problem. – Jason Apr 03 '19 at 09:03
  • As mentioned in my previous (edited) comment, the `CompletableFuture` does not hold a reference to either the actual work or the `Thread` processing it. This is at least implied in the class documentation: "_Since (unlike FutureTask) this class has no direct control over the computation that causes it to be completed, cancellation is treated as just another form of exceptional completion_". – Slaw Apr 03 '19 at 09:06
  • @Slaw yeah, `CompletableFuture` does not hold reference to thread, which I think it is also a problem, It is not consistency with `Future`. – Jason Apr 03 '19 at 09:13
  • @Slaw Agree, I changed the title. – Jason Apr 03 '19 at 09:26

0 Answers0