intermediateConcurrency Utilities

What is ExecutorCompletionService, and when is it useful?

ExecutorCompletionService wraps an underlying ExecutorService together with an internal BlockingQueue. As each submitted task finishes, its resulting Future is placed onto that queue in the order the tasks actually completed, rather than the order they were submitted in. Calling take() blocks until the next completed result becomes available, while poll() returns null immediately if nothing has finished yet. This is especially useful when you submit a batch of independent tasks and want to start processing each result as soon as it's ready, without being stuck waiting for a slower task to finish before you can look at the results of faster ones -- for example, submitting ten HTTP fetches and rendering each page as it arrives instead of waiting for the single slowest request to complete before showing anything.

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to What is the ABA problem in lock-free programming, and how do you solve it?← Back to all Java Concurrency & Multithreading questions