Java ExecutorService example using Callable and Future
GIAN Tutorials
8:21 AM
Callable interface represents a thread that can return a value. It is very much similar to Runnable interface except that it can return a value. Callable interface can be used to compute status or results that can be returned to invoking thread. For example: Let’s say you want to perform factorial and square of some numbers, you can do it concurrently using callable interface which will return value too. Callable defines only one method as below 1 2 3 4 5 6 public interface Callable { V call ( ) throws Exception ; } You can define the task which you want to perform inside call method. If it executes successfully, call method will return the result else it must throw an exception. You can use ExecutorService’s submit to execute Callable task. Let’s see Signature of submit method in ExecutorService. 1 2 3 Future submit ( Callable task ) ; If you notice, return