How to signal CompletableFutures as completed or canceled from their associated threads?

I read a lot about CompletableFutures, and they focus on the fact that CompletableFuture does not have access to the underlying computational code, as Future does. So, how can you signal complete () or cancel () (or check isCancelled () if you want to interrupt the calculation from the outside) from any task for which the terminated Future is connected?

EDIT: One of the things that bothers me is that CFs are used compared to the alternatives for their composition or for manual tuning, which seem pretty orthogonal to me when implemented, for example:

CompletableFuture.runAsync(() -> { 
                              if(myCondition) CF_REF?.complete();
                              else            CF_REF?.exceptionally();
                           }).thenApply(() -> {
                                        if (myOtherCondition) CF_REF_2?.complete();
 (...)

What is the point of developing it so that it is not "completed" + "compositional" at the same time?

I would like to find a way to use them as if CF used an interface similar to the hypothetical CompletableCallable as an interface, but I don’t know how to do it. Something like Function<CompletableFuture,T>instead of Callables / Runnables, so we could use it like:

CompletableFuture CF_REF_BIS = CompletableFuture.runAsync((CF_REF) -> { 
                                  if(myCondition) CF_REF.complete();
                                  else CF_REF.exceptionally();
                               });

CF_REF_BIS.thenApply(...)

When the code is calculated, the CF_REF_BIS reference itself will be returned by the internal execution mechanization, since it is actually passed as CF_REF in the future calculation without the need to access it by regions.

This means that we can reuse the de-anonymize calculation code simply by creating a new CompeltableCallable () and then sending it n times to any place where any thread has access to the CompletableFuture semaphore

For example:

CompletableFuture CF_REF = CompletableFuture.runAsync(myCompletableCallable)
             .thenApply(myCompletableCallable) //again
             .thenApply(anotherCompletableCallable); 

? - ? ? Java CompletedFuture ?

+4
1

javadoc:

CompletableFuture#complete()

, , get() .

CompletableFuture#cancel()

, CompletableFuture CancellationException. CompletableFuture, , , CompletionException, CancellationException.

[...]

:

mayInterruptIfRunning - , .

CompletableFuture , . , . .

whenXyz thenAbc CompletableFuture. , : ( ) , , *Async .. javadoc.


, CompletableFuture . , complete. . - , .

public static void main(String[] args) throws Exception {
    CompletableFuture<String> promise = new CompletableFuture<>();
    ExecutorService executorService = Executors.newFixedThreadPool(3);
    Runnable action = () -> {
        if (promise.complete("done")) {
            System.out.println("completed by " + Thread.currentThread());
        } else {
            System.out.println("somebody got there first");
        }
    };
    executorService.submit(action);
    executorService.submit(action);
    executorService.submit(action);

    executorService.shutdown();
    executorService.awaitTermination(100, TimeUnit.MILLISECONDS);
    System.out.println(promise.get());
}
+2

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


All Articles