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 ...

Get Java 9 Concurrency Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.