Callable Interface in Java

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.

Related blog:

Spring boot hibernate crud example

Recommended Posts

Callable Interface in Java
java (interface)
[java] interface
Access the network interface in Java
About Java interface
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
[Java] About interface
[Java] Functional interface
About interface, java interface
Pi in Java
FizzBuzz in Java
Understand java interface in your own way
About Java functional interface
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
java standard functional interface
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
JAVA learning history interface
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Basics of threads and Callable in Java [Beginner]
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Java learning memo (interface)
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
Use of Abstract Class and Interface properly in Java
Advanced inheritance abstract, interface -java
Try using RocksDB in Java
Read binary files in Java 1
Avoid Yubaba's error in Java
JAVA learning history interface inheritance
[Java] Functional interface / lambda expression
Get EXIF information in Java