zoukankan      html  css  js  c++  java
  • java动态线程池LinkedBlockingQueue和SynchronousQueue比较

    import java.util.concurrent.Callable;
    
    public class MyCallable implements Callable<String> {
        private String name; 
        public MyCallable(String name){ 
            this.name=name;
        }
        @Override
        public String call() throws Exception {
            System.out.println("Thread begin "+name);
            Thread.sleep(1000);
            return name;
        }
    }
    import java.util.concurrent.CompletionService;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorCompletionService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.LinkedBlockingQueue;
    import java.util.concurrent.SynchronousQueue;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    
    
    public class DynamicThreadPool {
        public static void main(String[] args) {
            /*ThreadPoolExecutor taskExecutor  = new ThreadPoolExecutor(
                    1,100,5, TimeUnit.SECONDS,
                    new LinkedBlockingQueue<Runnable>(100), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());*/
            ThreadPoolExecutor taskExecutor  = new ThreadPoolExecutor(
                    1,100,5, TimeUnit.SECONDS,
                    new SynchronousQueue<Runnable>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());
            long a=System.nanoTime();
             CompletionService<String> cs = new ExecutorCompletionService<>(taskExecutor);
                for (int i = 0; i < 100; i++) {
                    cs.submit(new MyCallable("Thread "+i));
                }
                for (int i = 0; i < 100; i++){
                     try {
                        System.out.println(cs.take().get());
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }  
                }
            System.out.println(System.nanoTime()-a);
        }
                          任务数(1任务耗时1秒)       用时(纳秒)       队列长度
    SynchronousQueue            100                 1010688455        
    LinkedBlockingQueue         100                   太长             100
    SynchronousQueue            1000                12041095086       
    LinkedBlockingQueue         1000                10073587003        100
    SynchronousQueue            10000               129230529016      
    LinkedBlockingQueue         10000               100583218224       100
  • 相关阅读:
    Haskell Interactive Development in Emacs
    Access Java API in Groovy Script
    手工设置Eclipse文本编辑器的配色
    Color Theme of Emacs
    Gnucash的投资记录
    Special Forms and Syntax Sugars in Clojure
    Use w3m as Web Browser
    SSE指令集加速之 I420转BGR24
    【图像处理】 增加程序速度的方法
    TBB 入门笔记
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/7280400.html
Copyright © 2011-2022 走看看