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

  • Home
  • SEARCH
  • 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 4255616
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:14:35+00:00 2026-05-21T05:14:35+00:00

I just found CompletionService in this blog post . However, this does’t really showcases

  • 0

I just found CompletionService in this blog post. However, this does’t really showcases the advantages of CompletionService over a standard ExecutorService. The same code can be written with either. So, when is a CompletionService useful?

Can you give a short code sample to make it crystal clear? For example, this code sample just shows where a CompletionService is not needed (=equivalent to ExecutorService)

    ExecutorService taskExecutor = Executors.newCachedThreadPool();
    //        CompletionService<Long> taskCompletionService =
    //                new ExecutorCompletionService<Long>(taskExecutor);
    Callable<Long> callable = new Callable<Long>() {
        @Override
        public Long call() throws Exception {
            return 1L;
        }
    };

    Future<Long> future = // taskCompletionService.submit(callable);
        taskExecutor.submit(callable);

    while (!future.isDone()) {
        // Do some work...
        System.out.println("Working on something...");
    }
    try {
        System.out.println(future.get());
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
  • 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-05-21T05:14:36+00:00Added an answer on May 21, 2026 at 5:14 am

    With ExecutorService, once you have submitted the tasks to run, you need to manually code for efficiently getting the results of the tasks completed.

    With CompletionService, this is pretty much automated. The difference is not very evident in the code you have presented because you are submitting just one task. However, imagine you have a list of tasks to be submitted. In the example below, multiple tasks are submitted to the CompletionService. Then, instead of trying to find out which task has completed (to get the results), it just asks the CompletionService instance to return the results as they become available.

    public class CompletionServiceTest {
    
            class CalcResult {
                 long result ;
    
                 CalcResult(long l) {
                     result = l;
                 }
            }
    
            class CallableTask implements Callable<CalcResult> {
                String taskName ;
                long  input1 ;
                int input2 ;
    
                CallableTask(String name , long v1 , int v2 ) {
                    taskName = name;
                    input1 = v1;
                    input2 = v2 ;
                }
    
                public CalcResult call() throws Exception {
                    System.out.println(" Task " + taskName + " Started -----");
                    for(int i=0;i<input2 ;i++) {
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e) {
                            System.out.println(" Task " + taskName + " Interrupted !! ");
                            e.printStackTrace();
                        }
                        input1 += i;
                    }
                    System.out.println(" Task " + taskName + " Completed @@@@@@");
                    return new CalcResult(input1) ;
                }
    
            }
    
            public void test(){
                ExecutorService taskExecutor = Executors.newFixedThreadPool(3);
                CompletionService<CalcResult> taskCompletionService = new ExecutorCompletionService<CalcResult>(taskExecutor);
    
                int submittedTasks = 5;
                for (int i=0;i< submittedTasks;i++) {
                    taskCompletionService.submit(new CallableTask (
                            String.valueOf(i), 
                                (i * 10), 
                                ((i * 10) + 10  )
                            ));
                   System.out.println("Task " + String.valueOf(i) + "subitted");
                }
                for (int tasksHandled=0;tasksHandled<submittedTasks;tasksHandled++) {
                    try {
                        System.out.println("trying to take from Completion service");
                        Future<CalcResult> result = taskCompletionService.take();
                        System.out.println("result for a task availble in queue.Trying to get()");
                        // above call blocks till atleast one task is completed and results availble for it
                        // but we dont have to worry which one
    
                        // process the result here by doing result.get()
                        CalcResult l = result.get();
                        System.out.println("Task " + String.valueOf(tasksHandled) + "Completed - results obtained : " + String.valueOf(l.result));
    
                    } catch (InterruptedException e) {
                        // Something went wrong with a task submitted
                        System.out.println("Error Interrupted exception");
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        // Something went wrong with the result
                        e.printStackTrace();
                        System.out.println("Error get() threw exception");
                    }
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just found that not only does this code compile, it seems to split
Just found this great article: http://www.blog.magepsycho.com/updating-product-qty-in-magento-in-an-easier-faster-way/ but something works not very well. When I
Just found out that this site uses tables for layouts. Does anyone find this
I just found this in a laravel bundle that I just installed, and really
I just found out this weird behavior, is this a bug or what am
Just found that unfinished manual, but it's really unfineshed. Right on the climax. I
I just found this: http://www.emadar.com/fpc/lockfree.htm at first glance it looks fine. Is anybody using
I just found that this will work: echo $value , " continue"; but this
I just found that when it comes to templates this code compiles in g++
just found this bit, while reading eclipse JDT 's documentation: IMethodBinding.getParameterTypes() : . .

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.