Separating the launching of tasks and the processing of their results in an executor

Normally, when you execute concurrent tasks using an executor, you will send Runnable or Callable tasks to the executor and get Future objects to control the method. You can find situations where you need to send the tasks to the executor in one object and process the results in another one. For such situations, the Java Concurrency API provides the CompletionService class.

The CompletionService class has a method to send tasks to an executor and a method to get the Future object for the next task that has finished its execution. Internally, it uses an Executor object to execute the tasks. This behavior has the advantage of sharing a CompletionService object ...

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.