Skip to content
CampusEduX

Multithreading · Lesson 105 of 150

Callable and Future

Callable is similar to Runnable, but its task can return a result and can throw a checked exception. Future represents the eventual result of a Callable task, which may still be running in the background when you first receive it.

1 min read

01What are Callable and Future?

Callable is similar to Runnable, but its task can return a result and can throw a checked exception. Future represents the eventual result of a Callable task, which may still be running in the background when you first receive it.

02Why is it used?

Runnable cannot return a value, but many real tasks need to produce a result — like a calculation running in the background whose result you need later. Callable and Future together support this "run now, collect result later" pattern.

03Real-Life Example

Think of placing an online order and receiving a tracking number (the Future) immediately, without waiting for the actual delivery. Later, you can check the tracking number to see if your order (the actual result) has arrived.

04Syntax

java
Callable<ResultType> task = () -> { return result; }; Future<ResultType> future = executor.submit(task); ResultType result = future.get(); // waits for and retrieves the result

05Example Program

java
import java.util.concurrent.*; public class CallableFutureDemo { public static void main(String[] args) throws Exception { ExecutorService executor = Executors.newSingleThreadExecutor(); Callable<Integer> task = () -> { return 10 * 10; }; Future<Integer> future = executor.submit(task); System.out.println("Result: " + future.get()); executor.shutdown(); } }

Output:

Result: 100

06Key Points to Remember

  • Callable returns a value; Runnable does not.
  • future.get() blocks (waits) until the result is ready, unless you check isDone() first.
  • Callable can throw checked exceptions, unlike Runnable's run() method.