00:00

RunnableFuture Interface in Java: A Task That Is Also a Future

Last updated:

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.

RunnableFuture.java (JDK declaration)
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

RunnableFuture interface hierarchyRunnableFuture extends java.lang.Runnable and Future. FutureTask implements RunnableFuture, and RunnableScheduledFuture extends it.«interface» · java.langRunnablerun()«interface»Future<V>get(), cancel(…)isDone(), isCancelled()«interface»RunnableFuture<V>this pagerun() «sets the result»«class»FutureTask<V>FutureTask(Callable)«interface»RunnableScheduledFuture<V>isPeriodic()extendsimplementsinterfaceclassjava.lang type

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 a Callable or a Runnable plus a result.
  • RunnableScheduledFuture (interface): Used by ScheduledThreadPoolExecutor for delayed and periodic tasks; it also extends ScheduledFuture.

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

ReportRenderJob.java
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());
    }
}
Output
Rendering started, done yet? false
Result: quarterly-report.pdf (12 pages)
Done now? true

How the code works

  1. new FutureTask<>(callable) creates the RunnableFuture.
  2. Because it is a Runnable, the Thread constructor accepts it directly.
  3. Because it is a Future, get() waits for the rendered report and isDone() reports progress.

5. Example: Building submit() on Any Executor

The same pattern gives any plain Executor a Future-returning submit() method.

SubmitOnAnyExecutor.java
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());
    }
}
Output
Word count: 5
Title     : ANNUAL REPORT

How the code works

  1. The static submit() helper wraps the Callable in a FutureTask, typed as RunnableFuture<T>.
  2. The executor runs the wrapper through execute(), the only method a plain Executor has.
  3. The helper returns the wrapper as a Future<T>, so the caller can call get().
  4. 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 FutureTask computes its result only once; calling run() again after completion does nothing.
  • Implementing it from scratch. Extend FutureTask instead; getting the completion and cancellation rules right is tricky.
  • Confusing it with CompletableFuture. A RunnableFuture is completed by running it; a CompletableFuture is completed by calling complete().
  • Prefer the executor API. In application code, ExecutorService.submit() already creates the RunnableFuture for you.

7. Summary

  • RunnableFuture<V> extends both Runnable and Future<V>.
  • Its run() method computes the task and sets the Future's result.
  • Executors use it to turn execute() into a Future-returning submit().
  • FutureTask is the standard implementation.

Related Tutorials

8. References