Returning value from Thread
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A Thread in Java does not directly return a value the way a normal method does. If you want a background computation to produce a result, the usual solution is to use Callable and Future, or a higher-level concurrency API such as CompletableFuture, instead of trying to force a return value out of Thread itself.
Why Thread Does Not Return A Result
The Thread API is built around Runnable, and Runnable.run() returns void.
That is why code like "start a thread and return its result" is not part of the raw Thread abstraction.
Use Callable And Future
Callable is the result-producing counterpart to Runnable.
This is the standard Java answer because it gives you:
- a return value
- exception propagation
- cancellation support
- coordination without manual shared-state hacks
Avoid Shared Mutable Variables As A Return Mechanism
People often try something like this:
This works in a narrow sense, but it is a poor pattern compared with Future because the value transport is hidden inside shared mutable state.
Use shared variables only when you truly need shared-state concurrency, not because you wanted a return value.
CompletableFuture For Modern Code
For newer Java code, CompletableFuture is often more expressive.
This is especially useful when background work needs to be chained, transformed, or combined with other asynchronous results.
join() And get() Are Different
With a raw Thread, join() only waits for completion. It does not retrieve a result.
With Future, get() both waits and returns the result.
That difference is the reason higher-level concurrency APIs are the right abstraction for value-returning tasks.
Exceptions Matter Too
Another benefit of Callable and Future is that task exceptions are surfaced through the future.
If you submit this to an executor, future.get() throws an ExecutionException wrapping the original cause.
That is far better than losing the failure in a background thread and wondering why the value never appeared.
Use Thread Only When You Need Thread Control
If the actual requirement is simply "do work concurrently and get a value back," then Thread is usually too low-level. ExecutorService, Future, and CompletableFuture are the real tools for that job.
Use raw Thread only when you specifically need to manage thread lifecycle directly.
Common Pitfalls
The biggest mistake is trying to make Thread.run() itself return a value. Another is using shared mutable containers as an improvised result channel when Future already solves the problem explicitly. Developers also often forget that join() only waits and does not transport a result. Finally, ignoring exceptions in worker threads can make a missing result look like a logic bug when it was really a failed computation.
Summary
- A raw Java
Threaddoes not directly return a value. - Use
CallableplusFuturewhen you want a result from background work. - '
CompletableFutureis often the cleaner modern choice for asynchronous result pipelines.' - Shared mutable variables are a weak substitute for proper result-handling APIs.
- Prefer higher-level concurrency abstractions unless you specifically need raw thread control.
Related reading
- RMI alternatives for bidirectional asynchronous calls and callbacks through firewalls or NAT
- Row was updated or deleted by another transaction or unsaved-value mapping was incorrect
- Ruby concurrency non-blocking I/O vs threads
- Run a process asynchronously and read from stdout and stderr
- Reverse a string in Java
- rmi registry binding issue
- Run async code before entire mocha test
- Run Bluebird Promises Sequentially, without return values?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.