zoukankan      html  css  js  c++  java
  • Java_并发线程_CompletionService

    1.CompletionService源代码分析

    CompletionService内部实现还是维护了一个可堵塞的队列,通过代理设计模式。从而操作队列。
        /**
         * Creates an ExecutorCompletionService using the supplied
         * executor for base task execution and a
         * {@link LinkedBlockingQueue} as a completion queue.
         *
         * @param executor the executor to use
         * @throws NullPointerException if executor is {@code null}
         */
        public ExecutorCompletionService(Executor executor) {
            if (executor == null)
                throw new NullPointerException();
            this.executor = executor;
            this.aes = (executor instanceof AbstractExecutorService) ?

    (AbstractExecutorService) executor : null; this.completionQueue = new LinkedBlockingQueue<Future<V>>(); //新建一个完毕队列 }

    	//通过submit提交Callable任务对象
        public Future<V> submit(Callable<V> task) {
            if (task == null) throw new NullPointerException();
            RunnableFuture<V> f = newTaskFor(task);
            executor.execute(new QueueingFuture(f));//线程池运行task对象
            return f;
        }
        /**
         * FutureTask extension to enqueue upon completion
         */
        private class QueueingFuture extends FutureTask<Void> {
            QueueingFuture(RunnableFuture<V> task) {
                super(task, null);
                this.task = task;
            }
            protected void done() { 
            	completionQueue.add(task); //运行玩后将task返回对象放置于完毕队列
            }
            private final Future<V> task;
        }
    	//通过take方法取得Future对象
        public Future<V> take() throws InterruptedException {
            return completionQueue.take();
        }


    2.实例

    	public static void main(String[] args) {
    
    		ExecutorService threadPool = Executors.newFixedThreadPool(3);
    		
    		CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(threadPool);
    		//将任务加入至threadPool池中。可是仅仅分配3个Thread对象
    		for (int i = 1; i <= 10; i++) {
    			final int seq = i;
    			completionService.submit(new Callable<Integer>() {
    				@Override
    				public Integer call() throws Exception {
    					Thread.sleep(new Random().nextInt(5000));
    					return seq;
    				}
    			});
    		}
    		
    		
    		for (int i = 0; i < 10; i++) {
    			try {
    				//completionService.take(), 至于call方法运行完毕,take堵塞採用数据
    				//future.get() 堵塞, 仅仅有当call运行完毕,
    				System.out.println(completionService.take().get());
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			} catch (ExecutionException e) {
    				e.printStackTrace();
    			}
    		}
    	}

  • 相关阅读:
    Bootstrap 4/3 页面基础模板 与 兼容旧版本浏览器
    Asp.net core 项目实战 新闻网站+后台 源码、设计原理 、视频教程
    C# 数据类型转换 显式转型、隐式转型、强制转型
    C# 多维数组 交错数组的区别,即 [ , ] 与 [ ][ ]的区别
    C#/Entity Frame Core 使用Linq 进行分页 .Skip() .Take() 的使用方法
    利用 Xunsearch 搭建搜索引擎、内容搜索实战
    Delphi
    Python
    Python
    Python
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5145206.html
Copyright © 2011-2022 走看看