O'Reilly logo

Java 9 Concurrency Cookbook - Second Edition by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required