The Completionservice interface is defined as the Interface completionservice<v> interface so that it has only one implementation executorcompletionservice in Java7, This interface integrates a blockingqueue, so it is possible to collect the results of multi-threaded operation. To better test the interface, I used two Tests, and the first test was to define an external blockingqueue to receive the data returned by callable. The second test is to decorate the executor with Completionservice so that the returned Completionservice object can submit the task directly.
But I found that it was not immediately called Executor's submit, but it was encapsulated, so there was a little delay. If you end up using the shutdown () command after the submit, the task may not have been placed in the executor Taskpool. So this is worth noting.
ImportJava.util.Random;ImportJava.util.concurrent.BlockingQueue;Importjava.util.concurrent.Callable;ImportJava.util.concurrent.CompletionService;Importjava.util.concurrent.ExecutionException;ImportJava.util.concurrent.ExecutorCompletionService;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;Importjava.util.concurrent.Future;ImportJava.util.concurrent.LinkedBlockingDeque; Public classtestcallable { Public Static voidMain (string[] args) {Try{futurecount (); Completionservicecount (); } Catch(interruptedexception e) {e.printstacktrace (); } Catch(executionexception e) {e.printstacktrace (); } } /*** Use a custom blocking queue to get task execution results * *@throwsinterruptedexception *@throwsexecutionexception*/ Public Static voidFuturecount ()throwsinterruptedexception, executionexception {blockingqueue<Future<Integer>> queue =NewLinkedblockingdeque<future<integer>>(); Executorservice Executorservice=Executors.newcachedthreadpool (); intThreadnum = 5; for(inti = 0; i < threadnum; i++) { future<Integer> future =Executorservice.submit (Gettask ()); Queue.put (future); } intsum = 0; inttemp = 0; while(!Queue.isempty ()) {Temp=Queue.take (). get (); Sum+=temp; System.out.print (Temp+ "\ T"); } System.out.println ("Blockingqueue All is:" +sum); Executorservice.shutdown (); } /*** Collect callable results using Completionservice *@throwsexecutionexception *@throwsinterruptedexception*/ Public Static voidCompletionservicecount ()throwsinterruptedexception, executionexception {executorservice executorservice=Executors.newcachedthreadpool (); Completionservice<Integer> Completionservice =NewExecutorcompletionservice<integer>(Executorservice); intThreadnum = 5; for(inti = 0; i < threadnum; i++) {Completionservice.submit (Gettask ()); } intsum = 0; inttemp = 0; for(inti=0;i<threadnum;i++) {Temp=Completionservice.take (). get (); Sum+=temp; System.out.print (Temp+ "\ T"); } System.out.println ("Completionservice All is:" +sum); Executorservice.shutdown (); } Public StaticCallable<integer>Gettask () {FinalRandom Rand =NewRandom (); Callable<Integer> task =NewCallable<integer>() {@Override PublicInteger Call ()throwsException {intnum = 0; for(inti = 0; I < 10; i++) {num= num + rand.nextint (10); } returnnum; } }; returntask; }}
Callable and Completionservice interface trial