该何时在ExecutorService上使用Completion

该何时在ExecutorService上使用Completion

本文介绍了我应该何时在ExecutorService上使用CompletionService?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚在。然而,这并不真正展示了CompletionService相对于标准ExecutorService的优点。相同的代码可以用任一种写。那么,什么时候是一个完成服务有用的?

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?

你能给出一个简短的代码示例,使它清楚吗?例如,此代码示例只显示不需要CompletionService的位置(=等价于ExecutorService)

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();
    }


推荐答案

提交要运行的任务,您需要手动代码以有效地获得任务的完成结果。使用CompletionService,这是几乎自动化。在您提交的代码中,区别不是很明显,因为您只提交一个任务。但是,假设您有一个要提交的任务列表。在下面的示例中,多个任务提交到CompletionService。然后,不是试图找出哪个任务已经完成(获取结果),它只是要求CompletionService实例在它们可用时返回resuts。

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 resuts 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");
                }
            }
        }
    }

这篇关于我应该何时在ExecutorService上使用CompletionService?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:52