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

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.