Recursively cancel all CompletableFuture

If i have

CompletableFuture<Something> future1 = service.request(param1);
CompletableFuture<Something> future2 = service.request(param2);
CompletableFuture<Void> many = CompletableFuture.allOf(future1, future2);

What will happen when I do many.cancel()? Will they be canceled future1and future2? If not, what would be the cleanest way to achieve this? I do not want to hold on future1and future2just to cancel them when I want to cancel many.

Some experience is why I want this: when I get some of the data, I need to request the corresponding, potentially future data to perform the calculation. If a newer piece of data arrives, I want to cancel the completion of the previous calculation, because the result will be immediately replaced by the new calculation.

+6
source share
3 answers

, , , CompletableFuture. , .

, CompletableFuture, , , CompletableFuture "", , , , .

Future s , , , CompletableFuture, . CompletableFuture.cancel(boolean):

:

mayInterruptIfRunning - , .

, future1, future2, , many, , cancel many. , , , , future1 future2, , , .

:

CompletableFuture<String> supply = CompletableFuture.supplyAsync(() -> {
    LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(2));
    System.out.println("supplying value");
    return "foo";
});
CompletableFuture<String> then = supply.thenApply(s -> {
    System.out.println("Evaluating next stage");
    return s;
});
CompletableFuture<?> last = then.handle((s,t) -> {
    System.out.println("last stage: value: "+s+", throwable: "+t);
    return "";
});
System.out.println("cancelling: "+supply.cancel(true));
ForkJoinPool.commonPool().awaitQuiescence(1, TimeUnit.DAYS);

:

last stage: value: null, throwable: java.util.concurrent.CompletionException: java.util.concurrent.CancellationException
canceling: true
supplying value

( )

, supply.cancel(true) then.cancel(true) true false; Supplier.

, , , , , CompletableFuture. , service.request(paramN) .

CompletableFuture, , , complete , , CompletableFuture , complete . , , cancel , , , .


, cancel many, cancel future1 future2 , .

+8

, CompletableFuture.allOf, CompletableFuture. , , , CompletableFutures ( JavaDocs).

, , CompletableFuture, , .

+4

: https://github.com/vsilaev/tascalate-concurrent

It provides both truly canceled implementations of CompletionStage (CompletableTask) and methods for combining them (Promises.all)

CompletionStage<Something> future1 = CompletableTask
  .complete(param1, myExecutor).thenApplyAsync(this::serviceCall);
CompletionStage<Something> future2 = CompletableTask
  .complete(param2, myExecutor).thenApplyAsync(this::serviceCall);

Promise<List<Something>> many = Promises.all(future1, future2);

Now you can call many.cancel(true), and the two future1and future2will be canceled (if not yet completed). Moreover, if any of the individual futures terminates exclusively, then the other will be automatically canceled (again, if it is not completed yet).

0
source

Source: https://habr.com/ru/post/1016523/


All Articles