skip to content

What is wrong with a teammate's claim that CompletableFuture.thenApply, unlike thenApplyAsync, runs the callback synchronously on the thread that created the future?

level: middleimportance: should knowfreq 48%

answer

  1. Creating ≠ completing ≠ caller thread
  2. Non-async → completing thread
  3. Already complete → caller thread
  4. Creating thread is irrelevant
  5. Blocking here hits the completing/pool thread

basics

~20 s

A non-async thenApply callback normally runs on the completing thread, whichever thread finishes the previous stage, or on the caller if that stage was already done. The thread that created the future is usually irrelevant.

solid answer

~40 s

The claim confuses 'creating thread' with 'completing thread.' For a non-async thenApply on a future that is not yet complete, the callback normally runs on whatever thread completes the upstream stage — that is typically a worker thread from supplyAsync's pool, not the thread that wrote the chain. It runs on the caller when the upstream is already complete as you attach it, so there is no completion event to wait for and the work happens inline. The Javadoc promises no more than the completing thread "or any other caller of a completion method", so a thread waking from join() can run it too. None of these is necessarily the 'creating' thread. The teammate's mental model would lead to wrong reasoning about which pool gets blocked.

go deeper

for a junior

Recognizes that thenApply is the non-async form and runs 'inline,' but may still conflate creating/completing/caller threads.

for a middle

Cleanly separates completing thread vs caller, and corrects the 'creating thread' misconception with the completed-future example.

for a senior

Connects the rule to which pool gets blocked and when to switch to *Async with a dedicated executor to protect critical threads.

for a principal

Codifies the threading contract in code review/guidelines so the team never reasons from the wrong thread variable in async pipelines.

## Three different 'threads' people conflate When reasoning about `thenApply`, three distinct threads get muddled: 1. **The creating thread** — the thread that wrote the code building the chain (`cf.thenApply(...)`). This is almost always **irrelevant** to where the callback runs. 2. **The completing thread** — the thread that actually finishes the *upstream* stage by producing its result. 3. **The caller thread** — the thread executing the `.thenApply(...)` call at the moment you attach it. ## What actually decides the callback's thread (non-async) For `thenApply` (the non-async form), the JDK applies the callback **as soon as the dependency is satisfied**, using whichever thread is available at that instant: - **Upstream not yet complete when you attach:** the dependency fires later, so the callback normally runs on the **completing thread** (case 2). Example: `CompletableFuture.supplyAsync(task, pool).thenApply(fn)` — `task` runs on a `pool` worker, and when it finishes, *that same worker* runs `fn` before returning to the pool. - **Upstream already complete when you attach:** there is nothing to wait for, so the callback runs **synchronously on the caller** (case 3), right inside the `thenApply(...)` call. Example: `CompletableFuture.completedFuture(x).thenApply(fn)` runs `fn` on whatever thread executes that line. - **Another thread gets there first:** the Javadoc promises only that a non-async action "may be performed by the thread that completes the current CompletableFuture, or by any other caller of a completion method". If completion races with the attach, either the attaching thread or the completing thread may run it, whichever claims it first, and a thread that wakes from `join()` or `get()` on that future can run pending callbacks too. Notice the **creating thread (case 1) never appears** in the rule unless it happens to coincide with the caller or a helping thread. So the teammate's phrasing ('the thread that created the future') is simply the wrong variable. ## Why the distinction matters If you believe the callback runs on the 'creating' thread, you'll reason incorrectly about *which* thread you might be blocking. In reality: - A blocking `thenApply` will block the **completing thread** — often a `supplyAsync` pool worker or a library/event-loop thread. That can starve that pool. - Or, in the already-completed case, it blocks the **caller** — which might be your request/main thread, surprising people who assumed their pipeline was 'async.' Getting this right is the basis for deciding when to switch to `thenApplyAsync(fn, executor)` to move the work onto a controlled pool. ## Corrected one-liner 'Non-async `thenApply` usually runs on the **completing** thread, or on the **caller** if the upstream is already complete — not necessarily on the thread that built the chain.'

  • Give a concrete case where thenApply runs on the caller thread.
    CompletableFuture.completedFuture(x).thenApply(fn) — the upstream is already complete, so fn runs synchronously on whatever thread executes that line.
  • Which thread does a blocking non-async callback most often endanger in practice?
    The completing thread — typically a supplyAsync pool worker or an event-loop/library thread — which is why you offload blocking work with an *Async overload and a dedicated executor.

  • Saying the non-async callback runs on the thread that created/built the chain — it usually runs on the completing thread or the caller.
  • Believing non-async always means synchronous-on-caller — the caller runs it mainly when the upstream is already complete.
  • Believing non-async always means asynchronous — it never schedules a separate task.

CompletableFuture thenApply and thenApplyAsync
Java 1.8

context