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; } |
1 2 3 |
Future submit(Callable task); |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package org.arpit.java2blog; import java.util.concurrent.Callable; public class PowerCalc implements Callable{ private double number; PowerCalc(double number) public Double call() throws Exception { { this.number=number; } @Override System.out.println("Calculating Square with "+Thread.currentThread().getName()); Thread.sleep(2000); return Math.pow(number, number); } } |
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 |
package org.arpit.java2blog; import java.util.concurrent.Callable; public class FactorialCalc implements Callable { private double number; FactorialCalc(double number) { public Double call() throws Exception { this.number = number; } @Override System.out.println("Calculating factorial with "+Thread.currentThread().getName()); Thread.sleep(5000); double fact = 1; for (int i = 2; i <= number; i++) { fact *= i; } return fact; } } |
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 35 36 37 38 39 |
package org.arpit.java2blog; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.Executors; public class FutureCallableMain { ExecutorService es=Executors.newFixedThreadPool(4); public static void main(String[] args) { System.out.println("Start : "); Future powerFuture20; powerFuture20=es.submit(new PowerCalc(20)); Future factorialFuture20; Future powerFuture25; Future factorialFuture25; factorialFuture25=es.submit(new FactorialCalc(25)); factorialFuture20=es.submit(new FactorialCalc(20)); powerFuture25=es.submit(new PowerCalc(25)); try { System.out.println("Square of "+25+" : "+powerFuture25.get()); System.out.println("Square of "+20+" : "+powerFuture20.get()); System.out.println("Factorial of "+20+" : "+factorialFuture20.get()); es.shutdown(); System.out.println("Factorial of "+25+" : "+factorialFuture25.get()); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } System.out.println("Done"); } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
Start : Calculating Square with pool-1-thread-1 Calculating factorial with pool-1-thread-2 Calculating Square with pool-1-thread-3 Square of 20 : 1.048576E26 Calculating factorial with pool-1-thread-4 Factorial of 20 : 2.43290200817664E18 Done Square of 25 : 8.881784197001253E34 Factorial of 25 : 1.5511210043330986E25 |