RunnableFuture Interface in Java: A Task That Is Also a Future
RunnableFuture is a small interface that joins two roles in one object: it is a Runnable, so a thread or executor can run it, and a Future, so callers can wait for its result.
You rarely declare it yourself, but it is the glue inside the Executor Framework. This tutorial explains why it exists and shows how executors use it.
1. What Is RunnableFuture?
RunnableFuture<V> is an interface in java.util.concurrent, added in Java 6. It extends both Runnable and Future<V> and declares one method, run(), whose documented job is to set the Future to the computation's result unless it has been cancelled.
public interface RunnableFuture<V> extends Runnable, Future<V> {
void run();
}
Its main implementation is FutureTask. RunnableScheduledFuture extends it for scheduled tasks and adds isPeriodic().
2. RunnableFuture Hierarchy
Swipe sideways to see the whole diagram.
Runnable(java.lang interface): Lets threads and executors run the object.Future(interface): Lets callers wait for, inspect or cancel the result.FutureTask(class): The standard implementation; wraps aCallableor aRunnableplus a result.RunnableScheduledFuture(interface): Used byScheduledThreadPoolExecutorfor delayed and periodic tasks; it also extendsScheduledFuture.
3. Why Does It Exist?
Executor.execute() accepts only a Runnable, yet submit() must return a Future. A RunnableFuture solves both at once: the executor runs it as a Runnable, and the caller holds the very same object as a Future.
This is exactly how AbstractExecutorService implements submit(): it calls the protected newTaskFor() method to create a RunnableFuture, passes it to execute(), and returns it.
4. Example: Running a Future on a Plain Thread
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.RunnableFuture;
public class ReportRenderJob {
public static void main(String[] args) throws InterruptedException, ExecutionException {
// One object that is both a Runnable (to start it) and a Future (to read the result)
RunnableFuture<String> renderJob = new FutureTask<>(() -> {
Thread.sleep(150); // render a 12-page PDF
return "quarterly-report.pdf (12 pages)";
});
Thread worker = new Thread(renderJob, "pdf-renderer"); // accepted because it is a Runnable
worker.start();
System.out.println("Rendering started, done yet? " + renderJob.isDone());
System.out.println("Result: " + renderJob.get()); // read like any Future
System.out.println("Done now? " + renderJob.isDone());
}
}
Rendering started, done yet? false Result: quarterly-report.pdf (12 pages) Done now? true
How the code works
new FutureTask<>(callable)creates the RunnableFuture.- Because it is a
Runnable, theThreadconstructor accepts it directly. - Because it is a
Future,get()waits for the rendered report andisDone()reports progress.
5. Example: Building submit() on Any Executor
The same pattern gives any plain Executor a Future-returning submit() method.
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.RunnableFuture;
public class SubmitOnAnyExecutor {
// The same idea AbstractExecutorService.submit() uses: wrap, execute, return as a Future
static <T> Future<T> submit(Executor executor, Callable<T> task) {
RunnableFuture<T> wrapper = new FutureTask<>(task);
executor.execute(wrapper); // Executor only knows how to run Runnables
return wrapper; // the caller sees a Future
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
Executor newThreadPerTask = command -> new Thread(command).start(); // a minimal Executor
Future<Integer> wordCount = submit(newThreadPerTask, () -> "Lorem ipsum dolor sit amet".split(" ").length);
Future<String> title = submit(newThreadPerTask, () -> "Annual Report".toUpperCase());
System.out.println("Word count: " + wordCount.get());
System.out.println("Title : " + title.get());
}
}
Word count: 5 Title : ANNUAL REPORT
How the code works
- The static
submit()helper wraps the Callable in aFutureTask, typed asRunnableFuture<T>. - The executor runs the wrapper through
execute(), the only method a plain Executor has. - The helper returns the wrapper as a
Future<T>, so the caller can callget(). - The lambda
command -> new Thread(command).start()is a complete Executor that starts one thread per task.
6. Common Mistakes and Best Practices
- Running it twice. A
FutureTaskcomputes its result only once; callingrun()again after completion does nothing. - Implementing it from scratch. Extend
FutureTaskinstead; getting the completion and cancellation rules right is tricky. - Confusing it with CompletableFuture. A RunnableFuture is completed by running it; a
CompletableFutureis completed by callingcomplete(). - Prefer the executor API. In application code,
ExecutorService.submit()already creates the RunnableFuture for you.
7. Summary
RunnableFuture<V>extends bothRunnableandFuture<V>.- Its
run()method computes the task and sets the Future's result. - Executors use it to turn
execute()into a Future-returningsubmit(). FutureTaskis the standard implementation.
Related Tutorials
8. References
Frequently Asked Questions (FAQ)
Q1. Why does RunnableFuture redeclare run()?
To document its stronger meaning: for a RunnableFuture, run() must set the Future to the result of the computation unless the task has been cancelled. The signature is the same as Runnable.run().
Q2. Which classes implement RunnableFuture?
FutureTask is the main public implementation. RunnableScheduledFuture extends the interface, and ScheduledThreadPoolExecutor uses an internal implementation of it for scheduled tasks.
Q3. Can I pass a RunnableFuture to Thread?
Yes. It is a Runnable, so new Thread(runnableFuture).start() works, and you can still read the result with get().
Q4. How do executors use RunnableFuture?
AbstractExecutorService.submit() calls newTaskFor() to wrap the task in a RunnableFuture, passes it to execute(), and returns it to the caller as a Future.
Q5. Is RunnableFuture the same as CompletableFuture?
No. A RunnableFuture completes when it is run. A CompletableFuture does not implement Runnable; it is completed explicitly with complete() or by an async task, and supports chaining with callbacks.