Skip to main content

Threads in Java - Callable and Future (Part 15)

We all know that we can create a thread either by extending the Thread class or by implementing the Runnable interface (see here). In both cases, we code our business logic in the run() method which has a return type void. It means we cannot return anything from the run() method. To support this feature, instead of the Runnable interface we can use Callable interface.

Callable vs Runnable

  • When we implement Runnable, we implement its run() method which does not return anything. While in the case of Callable, we implement its call() method which returns a result.
  • A tread cannot be created with a Callable, it can only be created with the Runnable.
  • The call() method can throw an exception while the run() method cannot.


When the call() method completes, its result must be stored in an object known to the main thread so that main thread can know about the result that thread returned. For this, a Future object can be used.

A Future object can be thought of an object that holds the result - it may not hold it right now but it will do so in the future - once the Callable returns.

Callable and Future do two different things - Callable like Runnable encapsulates the task that is supposed to run on another thread. On the other hand, a Future is used to store a result obtained from a different thread.

Code Example

Here, we are going to create a FactorialCalculator which is of type Callable. We will perform our computations in the call() method. This call() method will return the result which will be retrieved from the Future reference held by the main thread.


The output of the above code is
Factorial of 2 is: 2
Factorial of 5 is: 120
Future result is: 120 and Task done is: true
Future result is: 2 and Task done is: true
Factorial of 8 is: 40320
Future result is: 40320 and Task done is: true
Factorial of 9 is: 362880
Future result is: 362880 and Task done is: true
Here we are sending the Callable object to the executor using the submit() method. This method receives a Callable object as a parameter and returns a Future object that we can use with two main objectives -

  1. We can control the status of the task - we can cancel the task and check if it has finished. For this purpose, we have used the isDone() method to check if the task is finished.
  2. We can get the result returned by the call() - we are using get() method for this purpose. This method waits until the Callable object has finished the execution of the call() method and has returned its result.


Congratulations!! ๐Ÿ™‹ today we discussed Callable and Future and their implementations. I hope you enjoyed this post.

You can find the complete code of this project on my GitHub in this commit. Feel free to fork or open issues, if any.

I would love to hear your thoughts on this and would like have suggestions from you to make it better. 

Feel free to befriend me on FacebookTwitter or Linked In or say Hi by email.

Happy Coding ๐Ÿ˜Š


Popular posts from this blog

Parsing XML using Retrofit

Developing our own type-safe HTTP library to interface with a REST API can be a real pain as we have to handle many aspects - making connections caching retrying failed requests threading response parsing error handling, and more.  Retrofit, on the other hand, is a well-planned, documented and tested library that will save you a lot of precious time and headaches. In this tutorial, we are going to discuss how we can parse the XML response returned from  using the Retrofit library. To work with Retrofit, we need three classes -  Model class to map the JSON data Interfaces which defines the possible HTTP operations Retrofit.Builder class - Instance which uses the interface and the Builder API which allows defining the URL endpoint for the HTTP operation. Every method of the above interface represents on possible API call. The request type is specified by using appropriate annotations (GET, POST). The respon

Threads in Java - CountDownLatch (Part 12)

A CountDownLatch is a synchronizer which allows one thread to wait for one or more threads before starts processing. A good application of  CountDownLatch is in Java server-side applications where a thread cannot start execution before all the required services are started. Working A  CountDownLatch is initialized with a given count which is the number of threads it should wait for. This count is decremented by calling countDown() method by the threads once they are finished execution. As soon as the count reaches to zero, the waiting task starts running. Code Example Let us say we require three services, LoginService, DatabaseService and CloudService to be started and ready before the application can start handling requests. Output Cloud Service is up! Login Service is up! Database Service is up! All services are up. Now the waiting thread can start execution. Here, we can see that the main thread is waiting for all the three services to start before starting its own

Threads in Java - Masterclass (Part 0)

Threads in Java Multithreading is a way to introduce concurrency in a program. In any case, if there are parallel paths in our program (parts which do not depend on the result from another part), we can make use of multithreading. One should exploit this feature, especially with all these multiple core machines nowadays. Below are a few reasons why we should use multithreading - 1. Keep a process responsive There was once a time when you would print a document in MS Word and the application would freeze for an annoyingly long amount of time until the job finished. Eventually, Microsoft solved this problem by running a printing job parallel to the main thread/ GUI thread.  To be clear though, not only GUI apps but Network services have to keep an ear to the ground for new clients, dropped connections and cancellation requests. In either case, it is critical to do the heavy lifting on a secondary thread to keep the user satisfied. 2. Keep a processor busy Keeping a proc