Quick revision
The short answer
orTimeout(d, unit), added in Java 9, completes the future exceptionally with a TimeoutException if it has not completed within the given time. completeOnTimeout(value, d, unit) completes it with a default value instead. Both use an internal scheduler thread that fires after the delay and completes the future if it is still pending. Neither stops the underlying computation or request: it keeps running and its result is ignored, so we also set timeouts on the underlying client to free its resources.
What to remember
In this answer, we saw that orTimeout completes a pending future exceptionally with TimeoutException after the given time, and completeOnTimeout completes it with a default value instead. Both rely on a single shared scheduler thread that completes the future if it is still pending, so handlers that run on that thread should stay cheap. Neither stops the underlying computation or request, which keeps running while its result is ignored, so we set connect and read timeouts on the client as well and keep orTimeout as a backstop.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
orTimeout(d, unit), added in Java 9, completes the future exceptionally with a TimeoutException if it has not completed within the given time. completeOnTimeout(value, d, unit) completes it with a default value instead. Both use an internal scheduler thread that fires after the delay and completes the future if it is still pending. Neither stops the underlying computation or request: it keeps running and its result is ignored, so we also set timeouts on the underlying client to free its resources.
Why this question is asked
Before Java 9, putting a time limit on a CompletableFuture meant writing our own scheduler code or blocking with get(timeout). orTimeout and completeOnTimeout made it a one-liner, and they are now in almost every chain that calls a remote service. Interviewers ask about them because their behaviour is easy to misread: they look as if they cancel the slow work, and they do not. In this answer, we will see each method in action, look at how the internal scheduler triggers the timeout, show that the work keeps running, and then set timeouts in the right place.
1OrTimeout: Fail When Time Runs Out
orTimeout puts a limit on how long a future may stay pending. If the future is still incomplete when the time is up, it is completed exceptionally with java.util.concurrent.TimeoutException. If it completes in time, the timeout does nothing.
Notice one detail: orTimeout does not create a new stage. It sets the timeout on the same future and returns that same future. If that future is shared, every holder sees the timeout. Downstream stages see the failure as usual, wrapped in a CompletionException with the TimeoutException as its cause.
2CompleteOnTimeout: Use a Default Instead
completeOnTimeout is the gentler variant. Instead of failing, it completes the future with a value we provide when the time is up. This fits optional data where a default is perfectly acceptable, such as an empty list of recommendations.
So the choice is about meaning. If a late answer is an error the caller must know about, use orTimeout. If a late answer can quietly be replaced, use completeOnTimeout.
3How the Timeout Is Triggered
CompletableFuture has a small internal scheduler, a ScheduledThreadPoolExecutor with a single daemon thread, shared by all futures in the JVM. When we call orTimeout, it schedules a task to run after the delay. If the future completes first, that task is cancelled. If the delay passes first, the scheduler thread runs the task, which calls completeExceptionally(new TimeoutException()) on the future, or complete(value) for completeOnTimeout. Since complete only works on a future that is still pending, whichever happens first wins.
6 messages in order. Play them, or step through them one at a time.
The timeout ends the wait. The call itself carries on.
There is a practical consequence. Non-async stages attached to the future run on the thread that completes it. When the timeout wins, that is the scheduler thread, which is shared by the whole JVM. If an exceptionally handler after orTimeout does slow work, it holds up every other timeout in the process. Keep handlers after a timeout cheap, or use the Async form with an executor.
4The Work Keeps Running
Neither method stops the work behind the future. A CompletableFuture is only a holder for a result; it has no link to the thread or the request producing that result. So when the timeout fires, the supplyAsync task keeps running on its thread, and the HTTP request stays open, until it finishes on its own. Then it tries to complete the future, finds it already completed, and its result is thrown away.
Notice that pricing finished is logged after the caller has long since given up. Under load this matters a lot: if pricing is slow for everyone, each request leaves a thread and a connection busy for 3 seconds even though it waited only 500 ms. The pools fill up with abandoned work.
5Set the Timeout on the Client Too
To free resources, the timeout must reach the thing doing the work. HTTP clients have their own connect and read or response timeouts; database drivers have query timeouts. When those fire, the client closes or releases the connection and the thread returns to its pool. So we set the client timeout to match our budget and keep orTimeout as a backstop for anything the client does not cover.
Notice that the backstop is a little longer than the client timeout, so in the normal case the client's own timeout fires first and cleans up properly, and orTimeout only catches what slips through.
6Follow-up questions
What is the difference between orTimeout and get(timeout)?
get(500, MILLISECONDS) makes the calling thread wait at most 500 ms and throws TimeoutException to that caller only; the future itself stays pending. orTimeout completes the future itself, so every stage and every holder sees the timeout, and no thread has to block to enforce it.
Can I call orTimeout on a future I did not create?
Yes, but remember it changes that future in place. If a library shares one future between callers, our timeout would fail it for everyone. When in doubt, apply it to a copy, for example future.copy().orTimeout(...), so only our view of it times out.
Was there a way to do this before Java 9?
Only by hand: schedule a task on a ScheduledExecutorService that calls completeExceptionally after the delay, and cancel it when the future completes. orTimeout and completeOnTimeout do exactly that internally.
7Conclusion
In this answer, we saw that orTimeout completes a pending future exceptionally with TimeoutException after the given time, and completeOnTimeout completes it with a default value instead. Both rely on a single shared scheduler thread that completes the future if it is still pending, so handlers that run on that thread should stay cheap. Neither stops the underlying computation or request, which keeps running while its result is ignored, so we set connect and read timeouts on the client as well and keep orTimeout as a backstop.
Open with
Both were added in Java 9. orTimeout with a duration says: if this future is not complete by then, complete it exceptionally with a TimeoutException. completeOnTimeout does the same check but completes the future with a default value I give it, which suits optional data like recommendations.
Then, if they want more
Internally CompletableFuture has a small shared scheduler with one daemon thread; orTimeout schedules a task after the delay, and if the future is still pending then, that task completes it, and if the future completes first the task is cancelled.
Two details I keep in mind: orTimeout changes the same future and returns it, so it affects everyone holding that future, and when the timeout wins, non-async stages after it run on that shared scheduler thread, so they must be cheap. The important part is that neither method stops the underlying work.
The future is just a result holder, so the supplyAsync task or the HTTP request keeps running, holding a thread and a connection, and its result is ignored when it finally arrives.
That is why I always set timeouts on the client itself, connect and read or request timeouts, or a query timeout for the database, so resources really get freed, and I keep orTimeout slightly longer as a backstop.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- 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.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
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 "How do orTimeout and completeOnTimeout work (Java 9+), and do they stop the underlying...": https://scrivio-interview-library.netlify.app/java/completable-future/3b8fe43b20/