Aufrufbare Schnittstelle 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

Aufrufbare Schnittstelle in Java
Java (Schnittstelle)
[Java] -Schnittstelle
Greifen Sie mit Java auf die Netzwerkschnittstelle zu
Über die Java-Schnittstelle
Partisierung in Java
Änderungen in Java 11
Janken in Java
[Java] -Schnittstelle
[Java] Funktionsschnittstelle
Über Schnittstelle, Java-Schnittstelle
Umfangsrate in Java
FizzBuzz in Java
Verstehen Sie die Java-Oberfläche auf Ihre eigene Weise
Informationen zur Java-Funktionsschnittstelle
Lesen Sie JSON in Java
Interpreter-Implementierung durch Java
Machen Sie einen Blackjack mit Java
Janken App in Java
Einschränkungsprogrammierung in Java
Setzen Sie Java8 in Centos7
NVL-artiger Typ in Java
Verbinden Sie Arrays in Java
"Hallo Welt" in Java
Java-Standardfunktionstyp-Schnittstelle
Kommentare in der Java-Quelle
Azure funktioniert in Java
Formatieren Sie XML in Java
Einfache HTML-Spezialchars in Java
Boyer-Moore-Implementierung in Java
Hallo Welt in Java
Verwenden Sie OpenCV mit Java
WebApi-Memorandum mit Java
Typbestimmung in Java
Befehle in Java ausführen (Ping)
Verschiedene Threads in Java
Implementierung der Heap-Sortierung (in Java)
Zabbix API in Java
ASCII-Kunst in Java
Listen in Java vergleichen
POST JSON in Java
Fehler in Java ausdrücken
Erstellen Sie JSON in Java
Datumsmanipulation in Java 8
Was ist neu in Java 8?
Java-Lernnotiz (Schnittstelle)
Verwenden Sie PreparedStatement in Java
Was ist neu in Java 9,10,11
Parallele Ausführung in Java
Verwendung von Abstract Class und Interface in Java richtig
Zusammenfassung der erweiterten Vererbung, Schnittstelle -java
Versuchen Sie es mit RocksDB mit Java
Lesen Sie Binärdateien in Java 1
Vermeiden Sie den Fehler, den Yuma in Java gemacht hat
Vererbung der JAVA-Lernverlaufsschnittstelle
[Java] Funktionsschnittstelle / Lambda-Ausdruck
Holen Sie sich EXIF-Informationen in Java