Interface appelable en 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

Interface appelable en Java
java (interface)
interface [java]
Accéder à l'interface réseau avec Java
À propos de l'interface Java
Partition en Java
Changements dans Java 11
Janken à Java
Interface [Java]
[Java] Interface fonctionnelle
À propos de l'interface, interface java
Taux circonférentiel à Java
FizzBuzz en Java
Comprenez l'interface java à votre manière
À propos de l'interface fonctionnelle Java
Lire JSON en Java
Implémentation de l'interpréteur par Java
Faites un blackjack avec Java
Application Janken en Java
Programmation par contraintes en Java
Mettez java8 dans centos7
NVL-ish guy en Java
Joindre des tableaux en Java
"Hello World" en Java
interface de type de fonction standard java
Commentaires dans la source Java
Fonctions Azure en Java
Formater XML en Java
Simple htmlspecialchars en Java
Interface d'historique d'apprentissage JAVA
Implémentation Boyer-Moore en Java
Hello World en Java
Utiliser OpenCV avec Java
Mémorandum WebApi avec Java
Détermination de type en Java
Exécuter des commandes en Java (ping)
Divers threads en java
Implémentation du tri de tas (en java)
API Zabbix en Java
Art ASCII à Java
Comparer des listes en Java
POST JSON en Java
Exprimer l'échec en Java
Créer JSON en Java
Manipulation de la date dans Java 8
Nouveautés de Java 8
Mémo d'apprentissage Java (interface)
Utiliser PreparedStatement en Java
Nouveautés de Java 9,10,11
Exécution parallèle en Java
Utilisation correcte de la classe abstraite et de l'interface en Java
Résumé d'héritage avancé, interface -java
Essayez d'utiliser RocksDB avec Java
Lire des fichiers binaires en Java 1
Évitez l'erreur que Yuma a donnée en Java
Héritage de l'interface de l'historique d'apprentissage JAVA
[Java] Interface fonctionnelle / expression lambda
Obtenir des informations EXIF en Java