Javatpoint Logo
Javatpoint Logo

What is the Difference Between Future and Callable Interfaces in Java

In Java, the Future and Callable interfaces are important components of the java.util.concurrent package. They give you a mechanism to manage the outcomes of asynchronous tasks and work with asynchronous tasks. Although they both have similar functions, there are some significant variations between them that are important to recognise. In this article, we will delve into the dissimilarities between the Future and Callable interfaces in Java.

Purpose

Callable: The Callable interface is a functional interface introduced in Java 5 that represents a task or computation that returns a result. It is similar to the Runnable interface but allows a result to be returned.

Future: The Future interface, also introduced in Java 5, represents the result of an asynchronous computation. It provides methods to check if the computation is complete, wait for its completion, and retrieve the result.

Return Value

Callable: There is only one method, call(), on the Callable interface, and it returns a value of type V. The task must be completed and the computed result must be returned using the call() method.

Future: The Future interface provides methods to obtain the result of a computation performed by a Callable. The outcome is obtained using the get() method, which, if required, blocks until the computation is finished. V is the type argument of the Callable, and the result could be of type V.

Throwing Exceptions

Callable: The call() method of the Callable interface is allowed to throw checked exceptions. This means that the task performed by a Callable can have checked exceptions as part of its signature.

Future: The get() method of the Future interface throws checked exceptions, specifically ExecutionException and InterruptedExceptionInterruptedException is thrown if the current thread was interrupted while waiting for the computation to finish, while ExecutionException is thrown if the computation threw an exception.

Execution and Scheduling

Callable: To execute a Callable task, you need to submit it to an ExecutorService using the submit() method. A Future object that represents the outcome of the computation is returned by the submit() method.

Future: When a task is handed off to an ExecutorService, a Future object is obtained. It enables you to keep tabs on the computation's progress, stop it if necessary, and receive the outcome.

State and Blocking

Callable: The Callable interface is stateless. Each time you submit a Callable task, a new instance is created to execute that task.

Future: The Future interface has functions like isDone() and isCancelled() to check the status of the calculation. Additionally, it provides the get() method, which enables blocking while waiting for the computation to finish.

Cancelling Tasks

Callable: The Callable interface does not have a built-in mechanism for cancelling tasks. However, you can use the Future object returned by the ExecutorService's submit() method to cancel a task by calling the cancel() method on the Future object.

Future: The Future interface provides the cancel() method, which attempts to cancel the associated computation. It takes a boolean parameter that indicates whether the running task should be interrupted or not.

Timeout handling

You can set a timeout number in the Future interface's get() method to designate how long it should block while waiting for the outcome. A TimeoutException is thrown if the result is not accessible within the allotted time period. This gives you flexibility in addressing situations in which you don't want to have to wait around for a task to finish.

Multiple Results

The Callable interface allows you to return a single result using the call() method. However, if you need to return multiple results or update the result over time, you can use a more advanced interface called the CompletionService. The CompletionService builds on top of the Future interface and provides methods like submit() and take() to handle multiple asynchronous tasks and their results.

Asynchronous Execution

Both the Future and Callable interfaces enable asynchronous execution of tasks. When you submit a Callable task to an ExecutorService using the submit() method, it returns a Future object immediately. You can continue with other operations or tasks without waiting for the result. The get() method, which blocks until the calculation is finished, can be used to receive the outcome later.

Conclusion

In conclusion, while both the Future and Callable interfaces are closely related and play a significant role in working with asynchronous tasks and retrieving their results, they serve different purposes. The Future interface represents the outcome of an asynchronous calculation, whereas the Callable interface describes a job or computation that provides a result. Understanding the distinctions between these interfaces is crucial for effectively utilizing multithreading and asynchronous programming in Java.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA