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 task that has finished its execution. When the poll() method returns a Future object, it deletes it from the queue. In this case, you passed two attributes to the method to indicate the time you want to wait for the finalization of a task, in case the queue with the results of the finished tasks is empty.

Once the CompletionService object is created, you create two ReportRequest objects that execute a ReportGenerator task, execute a ReportGenerator task using the CompletionService object create before and passed as parameter to the constructor of the ReportRequest objects

..................Content has been hidden....................

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