Callable and Completionservice interface trial

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.