zoukankan      html  css  js  c++  java
  • CompletionService的好处与使用场景

    转自:https://blog.csdn.net/jdsjlzx/article/details/52912701

    FutureTask既是Future、Runnable,又是包装了Callable(如果是Runnable最终也会被转换为Callable ), 它是这两者的合体。

    package io.renren.test2;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.*;
    
    public class test2 {
    
        public static void main(String[] args) throws ExecutionException, InterruptedException {
            testFuture();
    //        testCompletionService();
        }
    
        //结果的输出和线程的放入顺序 有关(如果前面的没完成,就算后面的哪个完成了也得等到你的牌号才能输出!),so阻塞耗时
        public static void testFuture() throws InterruptedException, ExecutionException {
            System.out.println("--1.1--> main Thread begin:");
            ExecutorService executor = Executors.newCachedThreadPool();
            List<Future<String>> result = new ArrayList<Future<String>>();
            for (int i = 0; i < 10; i++) {
                Future<String> submit = executor.submit(new Task(i));
                result.add(submit);
            }
            executor.shutdown();
            for (int i = 0; i < 10; i++) {//一个一个等待返回结果
                System.out.println("--1.2--> 一个一个等待返回结果: " + result.get(i).get());
            }
            System.out.println("--1.3--> main Thread end:");
        }
    
        //结果的输出和线程的放入顺序 无关(谁完成了谁就先输出!主线程总是能够拿到最先完成的任务的返回值,而不管它们加入线程池的顺序),so很大大缩短等待时间
        private static void testCompletionService() throws InterruptedException, ExecutionException {
            System.out.println("--2.2--> main Thread begin:");
            ExecutorService executor = Executors.newCachedThreadPool();
            ExecutorCompletionService<String> completionService = new ExecutorCompletionService<>(executor);
            for (int i = 0; i < 10; i++) {
                completionService.submit(new Task(i));
            }
            executor.shutdown();
            for (int i = 0; i < 10; i++) {
                // 检索并移除表示下一个已完成任务的 Future,如果目前不存在这样的任务,则等待。
                Future<String> future = completionService.take(); //这一行没有完成的任务就阻塞
                System.out.println("--2.3--> " + future.get());   // 这一行在这里不会阻塞,引入放入队列中的都是已经完成的任务
            }
            System.out.println("--2.4--> main Thread end:");
        }
    
        private static class Task implements Callable<String> {
    
            private volatile int i;
    
            public Task(int i) {
                this.i = i;
            }
    
            @Override
            public String call() throws Exception {
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName());
                return "--0.1--> 任务 : " + i;
            }
    
        }
    }
    testFuture() 打印:
    --1.1--> main Thread begin:
    pool-1-thread-9
    pool-1-thread-2
    pool-1-thread-1
    pool-1-thread-3
    pool-1-thread-6
    pool-1-thread-7
    pool-1-thread-5
    pool-1-thread-8
    pool-1-thread-4
    --1.2--> 一个一个等待返回结果: --0.1--> 任务 : 0
    --1.2--> 一个一个等待返回结果: --0.1--> 任务 : 1
    --1.2--> 一个一个等待返回结果: --0.1--> 任务 : 2
    --1.2--> 一个一个等待返回结果: --0.1--> 任务 : 3
    --1.2--> 一个一个等待返回结果: --0.1--> 任务 : 4
    --1.2--> 一个一个等待返回结果: --0.1--> 任务 : 5
    --1.2--> 一个一个等待返回结果: --0.1--> 任务 : 6
    --1.2--> 一个一个等待返回结果: --0.1--> 任务 : 7
    --1.2--> 一个一个等待返回结果: --0.1--> 任务 : 8
    pool-1-thread-10
    --1.2--> 一个一个等待返回结果: --0.1--> 任务 : 9
    --1.3--> main Thread end:
    testCompletionService() 打印:
    --2.2--> main Thread begin:
    pool-1-thread-1
    --2.3--> --0.1--> 任务 : 0
    pool-1-thread-2
    pool-1-thread-7
    --2.3--> --0.1--> 任务 : 1
    --2.3--> --0.1--> 任务 : 6
    pool-1-thread-3
    --2.3--> --0.1--> 任务 : 2
    pool-1-thread-8
    --2.3--> --0.1--> 任务 : 7
    pool-1-thread-9
    --2.3--> --0.1--> 任务 : 8
    pool-1-thread-6
    pool-1-thread-5
    pool-1-thread-4
    --2.3--> --0.1--> 任务 : 5
    --2.3--> --0.1--> 任务 : 4
    --2.3--> --0.1--> 任务 : 3
    pool-1-thread-10
    --2.3--> --0.1--> 任务 : 9
    --2.4--> main Thread end:
    
  • 相关阅读:
    关于《货币金融学》若干问题的思考《九》
    关于《货币金融学》若干问题的思考《八》
    关于《货币金融学》若干问题的思考《七》
    关于《货币金融学》若干问题的思考《六》
    关于《货币金融学》若干问题的思考《五》
    关于《货币金融学》若干问题的思考《四》
    关于《货币金融学》若干问题的思考《三》
    关于《货币金融学》若干问题的思考《二》
    关于《货币金融学》若干问题的思考《一》
    XGBoost判断蘑菇是否有毒示例
  • 原文地址:https://www.cnblogs.com/hahajava/p/10838601.html
Copyright © 2011-2022 走看看