Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Streams and Functional

Runnable and Callable

Reference for Runnable and Callable in Java: run() versus call(), checked exceptions, return values, executors, Future.get() and how to choose.

Runnable and Callable<V> are the two functional interfaces that describe a unit of work. Runnable has a single method, void run(): it does something and returns nothing. Callable<V> has V call() throws Exception: it computes a result and is allowed to throw checked exceptions.

Both are used to hand work to somebody else: a Thread, an ExecutorService, a scheduler or a framework callback. Because each has exactly one abstract method, lambdas and method references fit both of them.

Java
Runnable r = () -> System.out.println("hello");
Callable<Integer> c = () -> 6 * 7;

Try it

Java Runnable versus Callable

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Submitting tasks to an executor

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
@FunctionalInterface
public interface Runnable {
    void run();
}

@FunctionalInterface
public interface Callable<V> {
    V call() throws Exception;
}
Runnable Callable<V>
Package java.lang java.util.concurrent
Method void run() V call() throws Exception
Returns a value No Yes
Checked exceptions Not allowed Allowed
Since Java 1.0 Java 5
Accepted by Thread Yes No

Executor methods that accept them:

Method Task Result
execute(Runnable) Runnable none; uncaught exceptions go to the thread's handler
submit(Runnable) Runnable Future<?> whose get() returns null
submit(Runnable, T) Runnable Future<T> that returns the value you supply
submit(Callable<T>) Callable Future<T> with the computed value
invokeAll(Collection) Callables list of completed Futures, in input order
invokeAny(Collection) Callables the result of one task that finished successfully

Usage notes

  • Calling run() directly does not start a thread. It is an ordinary method call. Use new Thread(task).start() or an executor to run the task concurrently.
  • Exceptions are swallowed until you ask. With submit(), an exception thrown by the task is stored in the Future and only appears, wrapped in ExecutionException, when you call get(). With execute() it reaches the thread's uncaught-exception handler.
  • Lambda ambiguity. A lambda such as () -> { throw new RuntimeException(); } fits both interfaces, and submit(() -> compute()) picks Callable when the lambda returns a value and Runnable when it does not. If both fit, cast or declare a typed variable.
  • InterruptedException. If a Runnable catches it, restore the flag with Thread.currentThread().interrupt() unless you are really finishing the thread's work.
  • Return type Void. Use Callable<Void> with return null; when you need a task that can throw checked exceptions but has no result.
  • Thread safety. Tasks that share mutable state need their own synchronization; the interfaces give none.
  • Executors.callable(Runnable) adapts a Runnable to a Callable<Object> that returns null.
  • Virtual threads (Java 21) accept the same Runnable tasks: Thread.ofVirtual().start(task) or Executors.newVirtualThreadPerTaskExecutor().