There's more...

The CompletionService class can execute Callable or Runnable tasks. In this example, you used Callable, but you could have also sent Runnable objects. Since Runnable objects don't produce a result, the philosophy of the CompletionService class doesn't apply in such cases.

This class also provides two other methods to obtain the Future objects of the finished tasks. These methods are as follows:

  • poll(): The version of the poll() method without arguments checks whether there are any Future objects in the queue. If the queue is empty, it returns null immediately. Otherwise, it returns its first element and removes it from the queue.
  • take(): This method, without arguments, checks whether there are any Future objects in the queue. If it is empty, it blocks the thread until the queue has an element. If the queue has elements, it returns and deletes its first element from the queue.

In our case, we used the poll() method with a timeout to control when we would like to end the execution of the ReportProcessor task.

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

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