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.
Runnable r = () -> System.out.println("hello"); Callable<Integer> c = () -> 6 * 7;
Try it
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.
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
@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. Usenew 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 theFutureand only appears, wrapped inExecutionException, when you callget(). Withexecute()it reaches the thread's uncaught-exception handler. - Lambda ambiguity. A lambda such as
() -> { throw new RuntimeException(); }fits both interfaces, andsubmit(() -> compute())picksCallablewhen the lambda returns a value andRunnablewhen it does not. If both fit, cast or declare a typed variable. InterruptedException. If aRunnablecatches it, restore the flag withThread.currentThread().interrupt()unless you are really finishing the thread's work.- Return type
Void. UseCallable<Void>withreturn 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 aRunnableto aCallable<Object>that returnsnull.- Virtual threads (Java 21) accept the same
Runnabletasks:Thread.ofVirtual().start(task)orExecutors.newVirtualThreadPerTaskExecutor().
Related
fi-overview,fi-supplier,fi-consumer- Lessons: Executors, Threads, Functional interfaces