• 根据CPU核数合理设置线程池大小


    一般来说池中总线程数是核心池线程数量两倍,只要确保当核心池有线程停止时,核心池外能有线程进入核心池即可。  

    我们所需要关心的主要是核心池线程的数量该如何设置。

    自定义线程池代码

    package com.lc.concurrent;
    import java.util.concurrent.ArrayBlockingQueue;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    
    public class MyThreadPoolExecutor {
        //最大可用的CPU核数
        public static final int PROCESSORS=Runtime.getRuntime().availableProcessors();
        //线程最大的空闲存活时间,单位为秒
        public static final int KEEPALIVETIME=60;
        //任务缓存队列长度
        public static final int BLOCKINGQUEUE_LENGTH=500;
    
        public ThreadPoolExecutor createThreadPool(){
            return new ThreadPoolExecutor(PROCESSORS * 2,PROCESSORS * 4,KEEPALIVETIME,TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(BLOCKINGQUEUE_LENGTH));
        }
    } 

    线程中的任务最终是交给CPU的线程去处理的,而CPU可同时处理线程数量大部分是CPU核数的两倍,运行环境中CPU

    的核数我们可以通过Runtime.getRuntime().availableProcessors()这个方法而获取。理论上来说核心池线程数量应该为

    Runtime.getRuntime().availableProcessors()*2,那么结果是否符合我们的预期呢,可以来测试一下(本次测试测试的

    是I/O密集型任务,事实上大部分的任务都是I/O密集型的,即大部分任务消耗集中在的输入输出。而CPU密集型任务主

    要消耗CPU资源进行计算,当任务为CPU密集型时,核心池线程数设置为CPU核数+1即可)

    package com.lc.concurrent;
    
    import java.util.Arrays;
    import java.util.Random;
    import java.util.concurrent.ThreadPoolExecutor;
    
    public class CreateThreads {
        public synchronized static void main(String[] args) {
            System.out.println(MyThreadPoolExecutor.PROCESSORS);
            new CreateThreads().test();
        }
    
        public synchronized void test(){
            ThreadPoolExecutor threadPoolExecutor=new MyThreadPoolExecutor().createThreadPool();
                for (int i = 0; i <= 100; i++) {
                    MyTask myTask = new MyTask(i);
                    threadPoolExecutor.execute(myTask);
                }
                threadPoolExecutor.shutdown();
            }
        }
    
    class MyTask implements Runnable{
        private int i;
    
        public MyTask(int i){
            this.i=i;
        }
    
        @Override
        public void run() {
            System.out.println("任务"+i+"开始执行"+System.currentTimeMillis());
            for (int i=0;i<32766;i++){
                Random random=new Random();
                int randNum=random.nextInt();
                int[] a={1,2,3,4,5,6,9,18,290,238,991,100,19,1932,randNum};
                Arrays.sort(a);
                Arrays.hashCode(a);
                Arrays.stream(a);
            }
            System.out.println("任务"+i+"结束执行"+System.currentTimeMillis());
        }
    }

    本机CPU核数为4,可同时处理8线程,测试结果如下:

       核心池线程数量     执行耗时(毫秒,多次测试结果以/间隔)
             4                   474/479/471
             8                   430/436/421
             12                  432/425/438
             16                  437/431/449
             20                  471/481/469
            

    可以发现当线程数量小于CPU核数两倍时速度明显较慢,超过两倍后速度差不多,当核心池数量过多时,速度又会显著下降

    由此可以看出,核心池线程数量大小应在CPU核数两倍以上且不宜过多。

    所以说,将线程池的核心池线程数量配置为CPU核数的两倍是比较合适的。



  • 相关阅读:
    关于回溯与招聘市场
    关于回溯与马
    关于回溯和后宫
    关于兔子
    关于递归和斐波那契数列
    关于递归和汉诺塔
    关于简单汉诺塔
    nodejs报错roll back,because of a error.node.js setup wizard ended prematurel
    fatal error C1859 意外的预编译头错误,只需重新运行编译器
    sqlserver2008 无法设置主体sa的凭据
  • 原文地址:https://www.cnblogs.com/coder-lichao/p/10931919.html
Copyright © 2020-2023  润新知