Java Callable And Future With Examples

In this post we’ll see two of the interesting features of the concurrent API, Callable and Future in Java.

Callable in Java

Consider a scenario where you have a big computation and you want to split it into multiple sub-tasks which are executed by multiple threads with each thread working on a part of the task. Once all the threads are finished with their tasks you can combine the partial results to get the result of the computation.
Designing this scenario with Runnable makes it difficult as Runnable doesn’t return a result. That shortcoming is filled by Callable in Java as it can return a result and may throw an exception.

Callable interface

Callable interface in Java has a single method call which computes a result and returns it or throws an exception if unable to do so.

So, you need to implement call() method to provide the task that has to be implemented by a thread as an asynchronous computation. Here is a simple example of Callable implementation-

Since Callable is a functional interface it can also be implemented as a lambda expression.

Running a Callable task using ExecutorService

To execute a Callable, submit() method of ExecutorService is used.

<T> Future<T> submit(Callable<T> task)– Submits a callable task which returns value for execution and returns a Future representing the pending results of the task.

When a callable task is submitted it is executed in its own thread asynchronously. It is not known when the result of that asynchronous computation will be available, all we know is it will be available in some future. Thus an appropriately named interface Future represents the return value of the callable task.

Java Callable and Future example

Here is a simple example showing how callable task is submitted using ExecutorService and how to obtain the returned value.

Output

As you can see the callable task is submitted for execution to execute in its own thread and the main thread resumes its execution (second System.out is executed immediately after callable submission).
Then the get method is called to retrieve the result of the computation, since get() is a blocking call so it waits for the computation to complete if required.

Future in Java

Future as we have already seen represents the result of an asynchronous computation.

Future interface provides methods to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation.

  • cancel(bollean interruptFlag)– Attempts to cancel execution of this task.
  • get()– Waits if necessary for the computation to complete, and then retrieves its result.
  • get(long timeout, TimeUnit unit)– Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.
  • isCancelled()– Returns true if this task was cancelled before it completed normally.
  • isDone()– Returns true if this task completed.

Callable and Future example

Here is an example of callable and future where 4 callable tasks are executed using the pool of two threads.

Output

As you can see from the output two callable tasks are executed by one thread and two tasks are executed by another thread from the thread pool.

In the example isDone() method is also used to check if the submitted task is completed or not.
That’s all for the topic Java Callable And Future With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.