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 the main class of the example, you created ThreadPoolExecutor using the newCachedThreadPool() method of the Executors class. Then, you used that Executor object to initialize a CompletionService object because the completion service uses an executor to execute its tasks. To execute a task using the completion service, use the submit() method, as in the ReportRequest class.

When one of these tasks is executed when the completion service finishes its execution, the service stores the Future object used to control its execution in a queue. The poll() method accesses this queue to check whether there is any task that has finished its execution; if yes, it returns the first element of the queue, which is a Future object of a ...

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