Understanding CompletableFuture :: runAsync

I just read the documentation about CompletableFuture::runAsyncand was rather confused by the explanation. Here is what it says:

Returns a new CompletableFutureone that asynchronously completes the task that is executed in this executor after the start of this action.

As I understand it, CompletableFutureit looks like Futureit can "register" some kind of callbacks and call them implicitly after this action is completed.

Given this, consider the following code:

ExecutorService threadsPool;
Runnable r;
//...
CompletableFuture.runAsync(r, threadsPool);

In this code we register Runnablewhich will be executed asynchronously in the given one ThreadPool.

But what does it mean , which asynchronously ends with the task . How can I complete the task ...? It makes no sense to me. CompletableFutureCompletableFuture

+4
source share
1 answer

Inside CompletableFuturethere is the following code called runAsync.

static CompletableFuture<Void> asyncRunStage(Executor e, Runnable f) {
    if (f == null) throw new NullPointerException();
    CompletableFuture<Void> d = new CompletableFuture<Void>();
    e.execute(new AsyncRun(d, f));
    return d;
}

AsyncRun- This is an asynchronously executed task that, after starting, Runnable fcompletes CompletableFuture dasynchronously. I will not worry about the code here because it is not very informative and just performs completion dby calling its method postComplete()(package-private).

+4
source

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


All Articles