Runnable
A task with no arguments and no result.
Available since: Java 1.0
Syntax
@FunctionalInterface
public interface Runnable {
void run();
}Examples
A task as a value:
void repeat(int times, Runnable action) {
for (int i = 0; i < times; i++) {
action.run();
}
}
void main() {
repeat(3, () -> IO.println("tick"));
}Output:
tick
tick
tickNotes
It is the oldest functional interface — threads (new Thread(runnable)) and ExecutorService tasks are built with it. The run() method cannot throw checked exceptions and returns no result; if you need a result use Callable or Supplier.
See also
- Thread.start() — Starts a thread: its code begins running in parallel with the caller.
- ExecutorService.submit() — Sends a task to the pool and immediately returns a `Future` for its result.
- Supplier — A supplier: a no-argument function that returns a value.
In the tutorial
- Functional interfaces — Functional interfaces in Java: the @FunctionalInterface annotation, the standard Function, Predicate, Consumer, Supplier and combining them.