When should I use a CompletionService over an ExecutorService?

后端 未结 10 2143
后悔当初
后悔当初 2020-11-28 01:52

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 c

10条回答
  •  清歌不尽
    2020-11-28 02:38

    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 {
                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 taskCompletionService = new ExecutorCompletionService(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 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");
                    }
                }
            }
        }
    

提交回复
热议问题