Published:
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;
/**
* Simple Java program to demonstrate how to use Callable and Future class in
* Java. You can use FutureTask for asynchronous processing.
*
*
*/
public class CallableAndFuture {
public static void main(String... args) throws InterruptedException, ExecutionException {
// creating thread pool to execute task which implements Callable
ExecutorService es = Executors.newSingleThreadExecutor();
System.out.println("submitted callable task to calculate factorial of 10");
Future result10 = es.submit(new FactorialCalculator(10l));
System.out.println("submitted callable task to calculate factorial of 15");
Future result15 = es.submit(new FactorialCalculator(15l));
System.out.println("submitted callable task to calculate factorial of 20");
Future result20 = es.submit(new FactorialCalculator(20l));
es.shutdown();
System.out.println("Calling get method of Future to fetch result of factorial 10");
Long factorialof10 = result10.get();
System.out.println("factorial of 100 is : " + factorialof10);
System.out.println("Calling get method of Future to get result of factorial 15");
Long factorialof15 = result15.get();
System.out.println("factorial of 15 is : " + factorialof15);
System.out.println("Calling get method of Future to get result of factorial 20");
Long factorialof20 = result20.get();
System.out.println("factorial of 20 is : " + factorialof20);
}
}
class FactorialCalculator implements Callable {
private Long number;
public FactorialCalculator(Long number){
this.number = number;
}
@Override
public Long call() throws Exception {
return factorial(number);
}
private long factorial(Long n) throws InterruptedException {
long result = 1;
while (n != 0) {
result = n * result;
n = n - 1;
Thread.sleep(100);
}
return result;
}
}
Output
submitted callable task to calculate factorial of 10
submitted callable task to calculate factorial of 15
submitted callable task to calculate factorial of 20
Calling get method of Future to fetch result of factorial 10
factorial of 10 is : 3628800
Calling get method of Future to get result of factorial 15
factorial of 15 is : 1307674368000
Calling get method of Future to get result of factorial 20
factorial of 20 is : 2432902008176640000
1) Callable is a SAM type interface, so it can be used in lambda expression.
2) Callable has just one method call() which holds all the code needs to executed asynchronously.
3) In Runnable interface, there was no way to return the result of computation or throw checked exception but with Callable you can both return a value and can throw checked exception.
4) You can use get() method of Future to retrieve result once computation is done. You can check if computation is finished or not by using isDone() method.
5) You can cancel the computation by using Future.cancel() method.
6) get() is a blocking call and it blocks until computation is completed.