Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

util.concurrent.Callable: practical reference

util.concurrent.Callable: practical reference: Callable<V> is a functional interface for a no-argument task that returns V and may throw Exception.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

java.util.concurrent.Callable

Callable is a functional interface for a no-argument task that returns V and may throw Exception.

Submit it to ExecutorService when a task produces a result or can throw a checked exception.

Cancelling the Future does not guarantee that the code stops; the task must cooperate with interruption.

call may legally return null, and Future.get exposes a failure as the cause of ExecutionException.

The example exercises the documented element in a small deterministic program and compares exact standard output.

Examples

public class Main {
    public static void main(String[] args) throws Exception {
        java.util.concurrent.Callable<Integer> task = () -> 42;
        System.out.println(task.call());
    }
}

Result

42

The example exercises the documented element in a small deterministic program and compares exact standard output.

Sources