Callable and CompletionService interfaces for trial use, completionservice

Source: Internet
Author: User

Callable and CompletionService interfaces for trial use, completionservice

The CompletionService interface is definedInterface CompletionService <V>The interface sets that it has only one ExecutorCompletionService implementation in Java 7. This interface integrates a BlockingQueue, so it can collect the results of multi-thread running. To better test this interface, I used two tests. The first test was to define an external BlockingQueue to receive the data returned by callable. In the second test, the executor is decorated with CompletionService so that the returned CompletionService object can directly submit the task.

However, I found that its submit does not immediately call the executor's submit, but encapsulates it, resulting in a little delay. If the shutdown () command is executed after the submit operation, the task may not be placed in the taskpool of the executor. So this is worth noting.

Import java. util. random; import java. util. concurrent. blockingQueue; import java. util. concurrent. callable; import java. util. concurrent. completionService; import java. util. concurrent. executionException; import java. util. concurrent. executorCompletionService; import java. util. concurrent. executorService; import java. util. concurrent. executors; import java. util. concurrent. future; import java. util. concurrent. linked BlockingDeque; public class testCallable {public static void main (String [] args) {try {futureCount (); completionServiceCount ();} catch (InterruptedException e) {e. printStackTrace ();} catch (ExecutionException e) {e. printStackTrace () ;}}/*** use a custom blocking queue to get the task execution result ** @ throws InterruptedException * @ throws ExecutionException */public static void futureCount () throws InterruptedException, Execu TionException {BlockingQueue <Future <Integer> queue = new LinkedBlockingDeque <Future <Integer> (); ExecutorService executorService = Executors. newCachedThreadPool (); int threadNum = 5; for (int I = 0; I <threadNum; I ++) {Future <Integer> future = executorService. submit (getTask (); queue. put (future);} int sum = 0; int temp = 0; while (! Queue. isEmpty () {temp = queue. take (). get (); sum + = temp; System. out. print (temp + "\ t");} System. out. println ("BlockingQueue all is:" + sum); executorService. shutdown ();}/*** use completionService to collect callable results * @ throws ExecutionException * @ throws failed */public static void completionServiceCount () throws InterruptedException, ExecutionException {ExecutorService executorService = Executors. newCachedThreadPool (); CompletionService <Integer> completionService = new ExecutorCompletionService <Integer> (executorService); int threadNum = 5; for (int I = 0; I <threadNum; I ++) {completionService. submit (getTask ();} int sum = 0; int temp = 0; for (int I = 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 static Callable <Integer> getTask () {final Random rand = new Random (); Callable <Integer> task = new Callable <Integer> () {@ Override public Integer call () throws Exception {int num = 0; for (int I = 0; I <10; I ++) {num = num + rand. nextInt (10) ;}return num ;}; return task ;}}

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.