DEV Community

Sharath Kumar
Sharath Kumar

Posted on

What is Callable and Future in Java?

In Java multithreading, tasks are usually executed using threads. The Runnable interface is commonly used to create tasks, but it has a limitation — it cannot return a result and cannot throw checked exceptions.

To overcome this limitation, Java introduced Callable and Future in the java.util.concurrent package.

These two concepts are widely used in concurrent programming and asynchronous task execution.


What is Callable?

Callable is an interface that represents a task that returns a result and can throw exceptions.

It is similar to Runnable, but with two key differences:

  • It returns a value
  • It can throw checked exceptions

Callable Interface Syntax

public interface Callable<V> {
    V call() throws Exception;
}
Enter fullscreen mode Exit fullscreen mode

The call() method is executed by a thread and returns a result.


Example of Callable

import java.util.concurrent.Callable;

class MyTask implements Callable<Integer> {

    public Integer call() {
        return 10 + 20;
    }

}
Enter fullscreen mode Exit fullscreen mode

Here the call() method returns 30.


What is Future?

Future is an interface that represents the result of an asynchronous computation.

When a Callable task is submitted to an ExecutorService, the result is returned as a Future object.

The Future object allows you to:

  • Check if the task is completed
  • Retrieve the result
  • Cancel the task

Example Using Callable and Future

import java.util.concurrent.*;

public class FutureExample {

    public static void main(String[] args) throws Exception {

        ExecutorService executor = Executors.newSingleThreadExecutor();

        Callable<Integer> task = () -> {
            return 50 + 50;
        };

        Future<Integer> result = executor.submit(task);

        System.out.println("Result: " + result.get());

        executor.shutdown();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Result: 100
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. A Callable task is created.
  2. It is submitted to ExecutorService.
  3. The Future object stores the result.
  4. future.get() retrieves the result.

Important Methods of Future

get()

Returns the result of the computation.

result.get();
Enter fullscreen mode Exit fullscreen mode

isDone()

Checks if the task is completed.

result.isDone();
Enter fullscreen mode Exit fullscreen mode

cancel()

Cancels the task.

result.cancel(true);
Enter fullscreen mode Exit fullscreen mode

isCancelled()

Checks whether the task was cancelled.

result.isCancelled();
Enter fullscreen mode Exit fullscreen mode

Difference Between Runnable and Callable

Feature Runnable Callable
Package java.lang java.util.concurrent
Return Value No return value Returns a value
Method run() call()
Exception Handling Cannot throw checked exceptions Can throw exceptions
Used With Thread ExecutorService

Real-World Use Cases

Callable and Future are commonly used in:

  • Parallel data processing
  • Web server request handling
  • Background tasks
  • Microservices communication
  • High-performance Java applications

They are especially useful when multiple tasks run in parallel and results need to be collected later.


Promotional Content

If you want to master advanced Java concepts like Multithreading, Concurrency, JVM Internals, and System Design, practical training is essential.

Join the Top System Design with Java Online Training program to learn how modern large-scale Java systems are designed and optimized.

This training program covers:

  • System Design Fundamentals
  • Java Concurrency and Multithreading
  • Scalable Backend Architecture
  • Microservices Design Patterns
  • Real-world system design case studies

Top comments (0)