I noticed that overriding of the cancel
method does not have effect if thenApply
is called on the future. The derived future is not aware of the fact that the original had such an override. The code below demonstrates the issue.
Is there any way to correctly override the method without losing possibility to transform the future?
import java.util.concurrent.CompletableFuture;
public class Main {
public static void main(String[] args){
CompletableFuture<Object> unmodified = new CompletableFuture<>(){
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
System.out.println("----> cancelling unmodified");
return super.cancel(mayInterruptIfRunning);
}
};
unmodified
.cancel(true);
CompletableFuture<Object> modified = new CompletableFuture<>(){
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
System.out.println("----> cancelling modified");
return super.cancel(mayInterruptIfRunning);
}
};
unmodified
.thenApply(x -> x)
.cancel(true);
System.out.println("----> end");
}
// OUTPUT
// > Task :Main.main()
// ----> cancelling unmodified
// ----> end
}