Example of ExecutorCompletionService in Java
December 23, 2012
ExecutorCompletionService extends CompletionService interface. We submit Callable threads to ExecutorCompletionService. Each thread will return a value. Now the role of ExecutorCompletionService is that we can apply the take method and take method will give only those thread’s value which has completed its task and has returned the value. To understand more we will discuss an example.
package com.concretepage; import java.util.concurrent.Callable; import java.util.concurrent.CompletionService; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorCompletionService; import java.util.concurrent.Executors; public class ExecutorCompletionServiceTest { public static void main(String... args) throws InterruptedException, ExecutionException{ Executor ex= Executors.newCachedThreadPool(); CompletionService<Long> cs = new ExecutorCompletionService<Long>(ex); cs.submit(new Worker()); cs.submit(new Worker()); cs.submit(new Worker()); for(int i=0;i<3;i++){ long l=cs.take().get(); //utilize the result System.out.println(l); } } } class Worker implements Callable{ @Override public Long call() throws Exception { //do some task and return back return System.currentTimeMillis(); } }