How it works...

In this recipe, you learned how to use the Callable interface to launch concurrent tasks that return a result. You implemented the FactorialCalculator class that implements the Callable interface with Integer as the type of the result. Hence, the call() method returns an Integer value.

The other critical point of this example is the Main class. You sent a Callable object to be executed in an executor using the submit() method. This method receives a Callable object as a parameter and returns a Future object that you can use with two main objectives:

  • You can control the status of the task you can cancel the task and check whether it has finished or not. For this purpose, you used the isDone() method.
  • You can get the result returned by the call() method. For this purpose, you used the get() method. This method waits until the Callable object has finished the execution of the call() method and has returned its result. If the thread is interrupted while the get() method is waiting for the result, it throws an InterruptedException exception. If the call() method throws an exception, then the get() method throws an ExecutionException exception as well.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.84.242