skip to content

How do you inspect a CompletableFuture's state without blocking, and what do getNow, isDone, isCancelled, and isCompletedExceptionally tell you?

level: middleimportance: should knowfreq 55%

answer

  1. isDone = settled any way (success/fail/cancel)
  2. isCancelled = cancelled only
  3. isCompletedExceptionally = failed OR cancelled
  4. success = isDone && !isCompletedExceptionally
  5. getNow(default): value, or default if pending, throws if failed
  6. snapshot only — check-then-act is racy

basics

~20 s

These methods peek at the future without waiting. isDone tells you if it's finished (any way). isCancelled and isCompletedExceptionally tell you how it finished. getNow(default) returns the value right now if ready, otherwise your default — it never blocks.

solid answer

~50 s

For non-blocking inspection: `isDone()` returns true if the future is settled in any way — success, exception, **or** cancellation. To distinguish how, `isCancelled()` is true only for cancelled futures, and `isCompletedExceptionally()` is true for **both** exceptional completion **and** cancellation (cancellation is a special exceptional completion via CancellationException). So a successful future is `isDone() && !isCompletedExceptionally()`; since Java 19, `state()` gives `RUNNING`, `SUCCESS`, `FAILED` or `CANCELLED` in one call. `getNow(valueIfAbsent)` is the non-blocking value reader: if the future is already completed successfully it returns the value; if it failed it throws (CompletionException/CancellationException); if it's still incomplete it returns your fallback immediately. These are mostly for monitoring, logging, fast-path optimizations, or speculative reads — not for control flow, because the state can change the instant after you check it (a check-then-act race). For real reactions to completion you should compose with whenComplete/handle rather than poll these flags.

go deeper

for a junior

Knows isDone tells whether the future finished and getNow returns a default if it's not ready yet.

for a middle

Distinguishes isCancelled vs isCompletedExceptionally and derives success = isDone && !isCompletedExceptionally; knows getNow throws on failure.

for a senior

Explains cancellation-as-exceptional-completion overlap, getNow's three outcomes, and why these are snapshots unsuitable for control flow (check-then-act races), and reaches for state(), resultNow() and exceptionNow() on Java 19 and later.

for a principal

Reserves inspection for observability/metrics and fast-path reads, mandates whenComplete/handle for actual reaction logic, and reasons about visibility/ordering between isDone and dependent-stage execution.

### Why inspect without blocking? `get()`/`join()` **block** until the future settles. Sometimes you just want to **peek** — for a dashboard, a log line, a fast path, or a speculative read — without parking the thread. CompletableFuture offers several non-blocking inspectors. ### isDone() `boolean isDone()` — true once the future is **settled in any way**: completed successfully, completed exceptionally, **or** cancelled. It does **not** tell you the outcome, only that the waiting is over. A subtle point: `isDone()` becomes true the moment the result is set, but **dependent stages may not have run yet** — it reflects the future's own state, not the whole chain. ### isCancelled() `boolean isCancelled()` — true **only** if the future holds a `CancellationException`: someone called `cancel(...)` and won the completion race, or completed it with `completeExceptionally(new CancellationException())`, which the class Javadoc gives as the same effect. False for normal success and for ordinary exceptional completion, including a stage **downstream** of a cancelled future: that stage fails with a `CompletionException` caused by the `CancellationException`, so it reports `isCancelled() == false`. ### isCompletedExceptionally() `boolean isCompletedExceptionally()` — true if the future ended in a failure **including cancellation**. This is the gotcha: **cancellation is modeled as an exceptional completion** (with a `CancellationException`), so a cancelled future reports `isCompletedExceptionally() == true` **and** `isCancelled() == true`. ### Deriving the full picture From these three you can classify any settled future: - **Success:** `isDone() && !isCompletedExceptionally()` - **Cancelled:** `isCancelled()` (also exceptional) - **Failed (non-cancel):** `isCompletedExceptionally() && !isCancelled()` - **Still running:** `!isDone()` ### Java 19 and later: state(), resultNow(), exceptionNow() Since Java 19, `Future` (and so `CompletableFuture`) gives the same picture in one call, still without blocking: - `state()` returns a `Future.State`: `RUNNING`, `SUCCESS`, `FAILED` or `CANCELLED`. A stage downstream of a cancelled future reports `FAILED`. - `resultNow()` returns the value of a future that completed successfully and throws `IllegalStateException` otherwise (running, failed or cancelled). - `exceptionNow()` returns the exception of a failed future, unwrapped from its `CompletionException`, and throws `IllegalStateException` if the future is running, succeeded or was cancelled. ```java switch (cf.state()) { case SUCCESS -> use(cf.resultNow()); case FAILED -> log(cf.exceptionNow()); case CANCELLED -> log("cancelled"); case RUNNING -> { } // still pending } ``` ### getNow(valueIfAbsent) ```java T getNow(T valueIfAbsent); ``` The non-blocking value reader: - Already completed **successfully** → returns the value. - Already completed **exceptionally** (incl. cancelled) → **throws** (`CompletionException` / `CancellationException`) — `getNow` is not exception-safe by itself. - **Not yet completed** → returns `valueIfAbsent` immediately, no blocking. Great for a fast path: "if the answer is already cached/ready, use it; otherwise use a placeholder and move on." ### The big caveat: check-then-act is racy All of these report a **snapshot** of a state that moves one way: an incomplete future can be completed or cancelled at any instant, while a done one stays done unless someone calls `obtrudeValue`/`obtrudeException`. So a `false` from `isDone()` is stale as soon as you read it: ```java if (!cf.isDone()) { cf.complete(defaultValue()); // may return false: another thread can complete cf between the check and this call } ``` Call `complete(...)` directly and read its `boolean` instead. And a `true` from `isDone()` does not say *how* it finished: `getNow(null)` on that future still throws if it failed. Use these for **observability and optimization**, not for coordinating logic. For reacting to completion, compose: ```java cf.whenComplete((value, ex) -> { /* runs exactly once, race-free */ }); ``` ### Quick example ```java CompletableFuture<String> cf = new CompletableFuture<>(); cf.isDone(); // false cf.getNow("pending"); // "pending" (not completed yet) cf.complete("ready"); cf.isDone(); // true cf.isCompletedExceptionally(); // false cf.getNow("pending"); // "ready" ```

  • Is isCompletedExceptionally() true for a cancelled future?
    Yes. Cancellation is modeled as exceptional completion with a CancellationException, so both isCancelled() and isCompletedExceptionally() are true.
  • What does getNow return on a still-incomplete future?
    It returns the valueIfAbsent argument immediately without blocking. It only returns the real value if already completed successfully, and throws if completed exceptionally.

  • Assuming isCompletedExceptionally is false for a cancelled future — it's true
  • Thinking getNow blocks or returns null on failure — it throws on failure
  • Using isDone() for control flow as if it were race-free
  • Believing isDone() means all dependent stages have also finished
  • Expecting isCancelled() to be true on a stage downstream of a cancelled future

CompletableFuture.getNow and isCompletedExceptionally
Java 1.8
Future.state, resultNow and exceptionNow
Java 19

Assumes JDK 27. Future.state(), resultNow() and exceptionNow() exist since Java 19, so on Java 8 to 18 only the boolean inspectors and getNow are available. JDK 25 and 27 agree.

context