Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8347261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:18:30+00:00 2026-06-09T07:18:30+00:00

If we use an ExecutorCompletionService we can submit a series of tasks as Callable

  • 0

If we use an ExecutorCompletionService we can submit a series of tasks as Callables and get the result interacting with the CompletionService as a queue.

But there is also the invokeAll of ExecutorService that accepts a Collection of tasks and we get a list of Future to retrieve the results.

As far as I can tell, there is no benefit in using one or over the other (except that we avoid a for loop using an invokeAll that we would have to submit the tasks to the CompletionService) and essentially they are the same idea with a slight difference.

So why are there 2 different ways to submit a series of tasks? Am I correct that performance wise they are equivalent? Is there a case that one is more suitable than the other? I can’t think of one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:18:33+00:00Added an answer on June 9, 2026 at 7:18 am

    Using a ExecutorCompletionService.poll/take, you are receiving the Futures as they finish, in completion order (more or less). Using ExecutorService.invokeAll, you do not have this power; you either block until are all completed, or you specify a timeout after which the incomplete are cancelled.


    static class SleepingCallable implements Callable<String> {
    
      final String name;
      final long period;
    
      SleepingCallable(final String name, final long period) {
        this.name = name;
        this.period = period;
      }
    
      public String call() {
        try {
          Thread.sleep(period);
        } catch (InterruptedException ex) { }
        return name;
      }
    }
    

    Now, below I will demonstrate how invokeAll works:

    final ExecutorService pool = Executors.newFixedThreadPool(2);
    final List<? extends Callable<String>> callables = Arrays.asList(
        new SleepingCallable("quick", 500),
        new SleepingCallable("slow", 5000));
    try {
      for (final Future<String> future : pool.invokeAll(callables)) {
        System.out.println(future.get());
      }
    } catch (ExecutionException | InterruptedException ex) { }
    pool.shutdown();
    

    This produces the following output:

    C:\dev\scrap>java CompletionExample
    ... after 5 s ...
    quick
    slow
    

    Using CompletionService, we see a different output:

    final ExecutorService pool = Executors.newFixedThreadPool(2);
    final CompletionService<String> service = new ExecutorCompletionService<String>(pool);
    final List<? extends Callable<String>> callables = Arrays.asList(
        new SleepingCallable("slow", 5000),
        new SleepingCallable("quick", 500));
    for (final Callable<String> callable : callables) {
      service.submit(callable);
    }
    pool.shutdown();
    try {
      Future<String> future;
      do {
        future = pool.isTerminated() ? service.poll() : service.take();
        if (future != null) {
          System.out.println(future.get());
        }
      } while (future != null);
    } catch (ExecutionException | InterruptedException ex) { }
    

    This produces the following output:

    C:\dev\scrap>java CompletionExample
    ... after 500 ms ...
    quick
    ... after 5 s ...
    slow
    

    Note the times are relative to program start, not the previous message.


    Test out a working example on Replit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Use OPENXML to get dt element in MSSQL 2005. How can I get xmlns:dt
I submit some some Future tasks using a CompletionService wrapped round a 2 thread
I have a series of concurrent tasks to run. If any one of them
use an icon (.png transparent background) for my menu. There isn't any problem for
Use case: we have some project meta-data files which we want tracked, but are
I would like to use the constructor on the ExecutorCompletionService which takes a pre-defined
use level1\level2\level3; Can someone explain with a simple demo ?
Is it safe to use singleton ExecutorService and multiple instances of CompletionService using the
use a reapeter to display data, but sometimes the data is to big to
Use of rails 3 recaptcha. In view _form.html.erb insert <%= recaptcha_tags %> But when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.