skip to content

What are the main ways to create a CompletableFuture that runs a task asynchronously, and how do supplyAsync and runAsync differ?

level: juniorimportance: must knowfreq 70%

answer

  1. supply = Supplier = value; run = Runnable = Void
  2. completedFuture = already done, no task
  3. new CompletableFuture<>() + complete() = manual
  4. default executor = common ForkJoinPool
  5. every async method has an Executor overload

basics

~10 s

Use CompletableFuture.supplyAsync when the task returns a value, and runAsync when it returns nothing. supplyAsync takes a Supplier and gives a CompletableFuture<T>; runAsync takes a Runnable and gives a CompletableFuture<Void>.

solid answer

~40 s

There are three common creation paths. CompletableFuture.completedFuture(value) builds one that is already finished with a known value—no task runs. CompletableFuture.supplyAsync(Supplier) runs a value-producing task on a background thread and returns a CompletableFuture<T> that completes with the supplier's result. CompletableFuture.runAsync(Runnable) runs a side-effecting task with no result and returns a CompletableFuture<Void>. The key distinction is the return: supplyAsync's Supplier produces a value you can later transform with thenApply etc.; runAsync's Runnable produces nothing, so its future completes with null and the natural next step is thenRun, which takes no input. Both run on the common ForkJoinPool by default, but each has an overload taking a custom Executor. You can also create an empty new CompletableFuture<>() and complete it manually later.

go deeper

for a junior

Knows supplyAsync returns a value (Supplier) and runAsync returns nothing (Runnable), and can pick the right one.

for a middle

Adds completedFuture and manual new+complete; explains the CompletableFuture<Void> consequence for chaining (thenRun vs thenApply).

for a senior

Notes the default common ForkJoinPool and the Executor overloads, and why blocking tasks need a dedicated executor.

for a principal

Reasons about pool sizing/starvation across the JVM, async API contracts (when to expose a CompletableFuture-returning method), and bridging external callbacks via manual completion.

## What a CompletableFuture is A `Future<T>` in Java represents a value that will be available *later*—the result of a computation running, typically, on another thread. The original `java.util.concurrent.Future` (Java 5) was limited: you could block in `get()`, poll `isDone()`, or `cancel()`, and the `resultNow()`, `exceptionNow()` and `state()` that Java 19 added only read an outcome that already exists. You could not say "when this finishes, do that" without a thread blocking or polling. `CompletableFuture<T>` (Java 8, in `java.util.concurrent`) fixes this. It implements `Future<T>` *and* `CompletionStage<T>`. "Completion stage" means you can attach **callbacks** that fire when it completes, building a non-blocking pipeline (`thenApply`, `thenCompose`, etc.). This question is only about how you **create** one. ## Term definitions - **Supplier<T>**: a functional interface with one method `T get()`—takes nothing, returns a value. Written as a lambda `() -> computeSomething()`. - **Runnable**: a functional interface with `void run()`—takes nothing, returns nothing (a side effect only). - **Executor**: an object that runs submitted tasks. It decides *which thread* runs the work. - **ForkJoinPool.commonPool()**: a shared, JVM-wide thread pool whose target parallelism defaults to the available processors minus one, and at least 1. It is the *default* executor for `CompletableFuture`'s async methods. ## The creation methods ### 1. `completedFuture(value)` ```java CompletableFuture<String> cf = CompletableFuture.completedFuture("hello"); ``` No task runs. The future is born *already complete* with the given value. `cf.get()` returns immediately. Useful for returning a constant from a method whose signature is async, or as a starting point in tests. ### 2. `supplyAsync(Supplier<T>)` — produces a result ```java CompletableFuture<Integer> cf = CompletableFuture.supplyAsync(() -> 2 + 2); ``` The supplier runs on a background thread (the common pool by default). When it returns, the future completes with that value (here `4`). Because there is a result, you can transform it: `cf.thenApply(x -> x * 10)`. ### 3. `runAsync(Runnable)` — no result ```java CompletableFuture<Void> cf = CompletableFuture.runAsync(() -> log.info("done")); ``` The runnable runs on a background thread but produces nothing, so the type is `CompletableFuture<Void>`. You chain steps that don't need an input value: `cf.thenRun(() -> ...)`. Its only value is `null`, so a `thenApply` compiles but its function receives `null`; `thenRun`, which takes no input, says what you mean. ### 4. Manual completion ```java CompletableFuture<String> cf = new CompletableFuture<>(); // ... later, from any thread/callback: cf.complete("value"); // or cf.completeExceptionally(ex) ``` You create an *empty, incomplete* future and complete it yourself when an external event (a callback, a message, a timer) arrives. This is the bridge between callback-style APIs and the CompletableFuture world. ## supplyAsync vs runAsync — the core difference | | `supplyAsync` | `runAsync` | |---|---|---| | Argument | `Supplier<T>` | `Runnable` | | Result type | `CompletableFuture<T>` | `CompletableFuture<Void>` | | Has a value? | Yes | No | | Next steps | `thenApply`, `thenCompose`… | `thenRun` | Choose by whether the work *yields a value you need downstream* (supply) or is a *fire-and-forget side effect* (run). ## Executor selection Every async creator has two overloads: ```java supplyAsync(supplier) // common ForkJoinPool supplyAsync(supplier, myExecutor) // your pool ``` Since JDK 25 the no-executor form uses the common pool on every host; earlier releases started a new thread per task when the pool's parallelism was below 2. The common pool is fine for short CPU-bound work but is **shared across the whole JVM**; long or blocking tasks (I/O, JDBC, sleeps) can starve it and stall unrelated code. For blocking work, pass a dedicated `Executor`.

  • After runAsync, what does a following thenApply receive?
    null. runAsync yields a CompletableFuture<Void>, and null is the only value of Void, so thenApply(v -> ...) compiles and runs with v == null; that is one way to start producing a value after a side effect. When the next step needs no input, thenRun says so directly.
  • When would you use completedFuture instead of supplyAsync?
    When the value is already known and no computation is needed—e.g. returning a cached/constant result from a method whose signature must be async, or seeding a pipeline/test.

  • Saying runAsync returns a value or takes a Supplier
  • Thinking supplyAsync blocks the calling thread until the result is ready
  • Believing each async call spins up a brand-new thread rather than using a shared pool
  • Confusing completedFuture (no task) with supplyAsync (runs a task)
  • Believing thenApply cannot be called on a CompletableFuture<Void>

CompletableFuture supplyAsync and runAsync
Java 1.8

Assumes JDK 27, the same at JDK 25: since JDK 25 the async methods without an Executor always use the common ForkJoinPool; before 25, a common pool with parallelism below 2 meant a new thread per task.

context