Callable and Future Interface :
Callable and Future interface has been introduced in JDK 1.5 under java.util.cocurrent package.
Callable interface is used to execute task and is similar to Runnable interface but can return value to caller and is able to throw checked Exception as well .Callable interface has call() method to execute task, computing result and returning result or throws an exception if unable to execute.
Callable interface is used by Executor framework, which contains pool of threads called worker threads then will call submit method of ExecutorService which is used to execute the Callable tasks by available threads in the pool.
When we submit callable task then returned result is an instance of Future object. This Future object is used to find out the status of callable task ,get the result of task computation or cancel the task.
Example using future and Callable :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class CallableDemo { private static final int WORKER_THREADS = 10; public static void main(String[] args) throws InterruptedException,ExecutionException { ExecutorService executor = Executors.newFixedThreadPool(WORKER_THREADS); Future<Integer> futureAdd = executor.submit(new AdditionCallable(10)); System.out.println("Computation result returned after submitting Callable task " + futureAdd.get()); executor.shutdown(); } } class AdditionCallable implements Callable<Integer> { Integer n; AdditionCallable(Integer n) { this.n = n; } @Override public Integer call() throws Exception { Integer sum = 0; for (int i = 0; i <= n; i++) { System.out.println("value added in sum is " + i); sum += i; System.out.println("Updated sum is " + sum); } return sum; } } |
OutPut:
value added in sum is 0
Updated sum is 0
value added in sum is 1
Updated sum is 1
value added in sum is 2
Updated sum is 3
value added in sum is 3
Updated sum is 6
value added in sum is 4
Updated sum is 10
value added in sum is 5
Updated sum is 15
value added in sum is 6
Updated sum is 21
value added in sum is 7
Updated sum is 28
value added in sum is 8
Updated sum is 36
value added in sum is 9
Updated sum is 45
value added in sum is 10
Updated sum is 55
Computation result returned after submitting Callable task 55
Methods of Callable and Future :
1) V call() throws Exception: This method returns result of executable task or throws an exception .
2) Get() :this method is used to get the result of callable task.It is a blocking call which means that if task is not finishes then it will wait until it is finished.
3) get(long timeout, TimeUnit): this method will wait at most the time specified in the method for the task to complete and give results if available.
4) Boolean Cancel() : this method will return true if attempt to cancel the task is successful. If task is already completed or cancelled then attempt will fail.
5) Boolean isCancelled(): this method will return true if task is cancelled before it gets complete.
6) Boolean isDone() : this method will return true if task compeltes.Completion of task can be exception,normal or cancelled.