What is Callable Interface in Java
For Java 5, the class “java.util.concurrent” was introduced. This callable interface was brought in via the concurrency package that looked similar to the Runnable interface. It also can return any object and is able to throw an Exception. A Java Callable interface uses Generics, thus making it possible to return any type of object. The Executor Framework gives a submit () method to execute Callable implementations in a pool of threads. In reality, the Java Executor Framework adhers to the WorkerThread patterns.
In a thread pool users can initiate threads by using the Executors.newFixedThreadPool(10); method and accordingly submit a task to it. A runnable acts as the target of a thread and a public void run () method is mandatorily implemented to define the task. This will be executed by threads in the thread pool. Based on the availability of the threads in the pool, the Executor Framework assigns work (runnable target) to threads. If all threads are in use, the task has to be stalled. After the thread completes one task then it returns to the pool as an available thread, which is ready to accept future tasks. Callable is similar to Runnable and can return any type of object when we want to get a result or status from the task.
Return of Callable Interface
Java Callable returns java.util.concurrent. Java Future offers a cancel () method to eliminate the associated Callable task. This is an overloaded version of the get () method, where one can specify a certain time to wait for the result. It is useful to avoid a current thread, which may be blocked for a longer period. Please remember that the get method is a synchronous method and until the callable finishes its task and returns a value, it will have to wait for a callable.
There are also “isDone()” and “iscancelled()” methods to fetch the current status of an associated Callable task. Consider the example where a sum of all numbers from one to 100 needs to be found. We can loop 1 to 100 sequentially and adding them finally. Another possibility is by dividing and conquering. In this method, we can group the numbers in a way such that each group has exactly two elements. Finally, we can assign that group to a pool of threads. Therefore, each thread returns a partial sum in parallel and then collects those partial sums and add them to get the whole sum.
Features of Callable and Future Class
Callable class is a SAM type interface and hence it can be implemented in the lambda expression.
The callable class has just one method “call ()” that holds all the code needed to execute asynchronously.
In a runnable interface environment, there was no possibility to return the result of the computation or throw checked exception. Whereas with Callable returning a value and throwing a checked exception is available.
Get () method of Future class can be used to retrieve results once the computation is done. Users can also check if the computation is finished or not by using done () method.
Canceling the computation by using the future. cancel () method is also a boon in some applications.
Get () is called a blocking call and it continues to block until the computation is completed.