Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Which thread runs the callback in cf.thenApply(f) compared with cf.thenApplyAsync(f) and cf.thenApplyAsync(f, executor)?

cf.thenApply(f) runs f on whichever thread completes cf, or on the caller's own thread if cf is already complete when thenApply is called. cf.thenApplyAsync(f) always submits f to the default async executor, usually ForkJoinPool.commonPool(), and cf.thenApplyAsync(f, executor) submits it to the executor we pass. Non-async callbacks are cheap but can run on unexpected threads, such as an HTTP client's I/O thread, so for blocking or long-running callbacks we choose the async variant with an explicit executor.

Why this question is asked

When we chain callbacks on a CompletableFuture, we rarely think about which thread runs them, and most of the time it does not matter. But when it does, it matters a lot: a slow callback on an HTTP client's I/O thread can stall every other request using that client, and a blocking callback on the common pool can starve parallel streams. Interviewers ask this to see whether we know the threading rules behind the API. In this answer, we will recap what a completion stage is, see with thread names where thenApply runs in each case, compare the two async variants, and then look at the practical consequences and how to choose.

1A Quick Recap of Completion Stages

A CompletableFuture holds a result that may not exist yet. Methods like thenApply register a dependent stage: a function to run when the result arrives, producing a new CompletableFuture with the transformed result. If the result is already there when we register the function, it can run immediately.

So there are two moments when a callback can run: when the original future completes, or when we register the callback, if the future was already complete. Which thread runs it depends on that moment and on whether we used an Async method.

2Where thenApply Runs

Let us complete a future from a thread we name completer, and register thenApply callbacks before and after completion, printing the thread name in each.

Java
CompletableFuture<String> cf = new CompletableFuture<>();
cf.thenApply(s -> log("registered before completion"));
Thread completer = new Thread(() -> cf.complete("data"), "completer");
completer.start(); completer.join();
// registered before completion -> runs on: completer
cf.thenApply(s -> log("registered after completion"));
// registered after completion -> runs on: main
Which thread runs the callback
Callcf not yet completecf already complete
thenApply(f)the thread that completes cfthe caller's thread, immediately
thenApplyAsync(f)default async executor (common pool)default async executor (common pool)
thenApplyAsync(f, executor)the given executorthe given executor

The first callback was waiting when the future completed, so the completing thread ran it as part of complete(). The second was registered on an already completed future, so the thread calling thenApply ran it immediately, before thenApply even returned. Notice that the same line of code can run on two different threads, depending on a race between completion and registration.

3Where the Async Variants Run

The Async variants never run the function on the completing thread or the caller's thread. thenApplyAsync(f) submits f as a new task to the future's default async executor, which for a plain CompletableFuture is ForkJoinPool.commonPool(), unless the common pool's parallelism is below 2, in which case a new thread is created for each task. thenApplyAsync(f, executor) submits f to the executor we pass.

Java
ExecutorService io = Executors.newFixedThreadPool(4, Thread.ofPlatform().name("io-", 0).factory());
cf.thenApplyAsync(s -> log("async, default"));
// async, default -> runs on: ForkJoinPool.commonPool-worker-1
cf.thenApplyAsync(s -> log("async, explicit"), io);
// async, explicit -> runs on: io-0

The same pattern applies to every stage method: thenAccept, thenRun, thenCompose, thenCombine, whenComplete, handle and exceptionally each have plain and Async forms with the same threading rules. Since Java 9, a subclass can override defaultExecutor() to change what the one-argument Async methods use.

4Why the Thread Matters

Non-async callbacks are cheap: no task submission, no queueing, no context switch. For small transformations such as mapping a response to a DTO, that is exactly what we want. But the cost of that efficiency is that we do not choose the thread.

Consider an asynchronous HTTP client. When the response arrives, the client completes the future on one of its own threads, often an I/O or event loop thread that serves many connections. A thenApply on that future runs our callback right there. If the callback parses a large document for 200 ms, or worse, calls a database, that I/O thread is busy and every other response it should handle waits. With a Netty-based client, one event loop thread may handle hundreds of connections.

Java
httpClient.sendAsync(request, BodyHandlers.ofString())
.thenApply(resp -> parseLargeXml(resp.body())) // runs on the client's thread: 200 ms of CPU there
.thenApply(doc -> repository.save(doc)); // blocking JDBC on the client's thread: worse
httpClient.sendAsync(request, BodyHandlers.ofString())
.thenApplyAsync(resp -> parseLargeXml(resp.body()), cpuPool) // CPU work on a CPU pool
.thenApplyAsync(doc -> repository.save(doc), ioPool); // blocking work on an I/O pool

The second chain says explicitly where each kind of work belongs. Using thenApplyAsync without an executor would move the work off the client's thread but onto the common pool, which is fine for short CPU work and wrong for blocking calls.

5Choosing Between Them

The rule of thumb is simple. Use the plain methods for short, non-blocking callbacks, a few microseconds of work, where running on any thread is safe. Use the Async methods with an explicit executor for anything that blocks, takes noticeable CPU time, or must run on a particular kind of thread. Avoid the one-argument Async methods in server code, because they put the work on the shared common pool without saying so.

There is one more reason to be deliberate. Thread-bound context, such as the logging MDC, a security context or a transaction, belongs to a thread. A callback that runs on an unexpected thread does not have the context of the thread that registered it. Executors that propagate context, as many platform starters provide, make the Async-with-executor variants the safer default for code that relies on such context.

6Follow-up questions

Can thenApply run on the thread that called it even if cf completes later?

Only if cf is already complete at the moment thenApply is called. If it completes afterwards, the completing thread runs the callback. Because this depends on timing, code must not rely on a callback running on a particular thread when it uses the plain methods.

Is thenApplyAsync slower than thenApply?

It adds a task submission and usually a thread hand-off, which costs microseconds, so for tiny transformations it is slower. The cost is worth paying when the callback blocks or is long, because it protects the thread that would otherwise run it.

What thread runs exceptionally() or whenComplete()?

The same rules apply. The plain forms run on the completing thread or the caller if already complete; exceptionallyAsync (since Java 12) and whenCompleteAsync run on the default or the given executor.

7Conclusion

In this answer, we saw that thenApply runs its function on the thread that completes the future, or immediately on the caller's thread if the future is already complete; that thenApplyAsync(f) submits the function to the default async executor, usually the common pool; and that thenApplyAsync(f, executor) uses the executor we give it. Plain callbacks are cheap but can land on threads like an HTTP client's I/O thread, so blocking or long callbacks should use the Async variant with an explicit executor.

What to say in the interviewAbout 100 seconds aloud

Open with

With plain thenApply, the function runs on whichever thread completes the future. So if an HTTP client completes it on its I/O thread, our function runs on that I/O thread as part of completion.

Then, if they want more

There is one exception: if the future is already complete when we call thenApply, the function runs immediately on our own calling thread, before thenApply returns.

That means the same line can run on different threads depending on timing. thenApplyAsync with just a function always submits it as a new task to the default async executor, which is ForkJoinPool.commonPool, unless the common pool's parallelism is below two, in which case it uses a new thread per task.

And thenApplyAsync with an executor submits the function to that executor, so we decide exactly where it runs. The same rules apply to thenAccept, thenCompose, handle and the others. In practice, plain callbacks are the cheapest, no hand-off, and they are right for small non-blocking transformations.

But because we do not choose the thread, a slow or blocking callback can end up on an HTTP client's event loop and stall every connection on it, or on the common pool and starve parallel streams.

So for anything that blocks or takes real CPU time, I use the Async variant with an explicit executor, an I/O executor for blocking calls and a CPU pool for heavy parsing, and I avoid the one-argument Async methods in server code. It also helps with context like MDC, because a context-propagating executor carries it over.

About this answer

Last updated
12 November 2026
Versions it names
Java 12, Java 9
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Which thread runs the callback in cf.thenApply(f) compared with cf.thenApplyAsync(f) and...": https://scrivio-interview-library.netlify.app/java/completable-future/7083438c93/