并发编程中线程池 是跑不了的, 用过线程池的朋友 都会遇到这样的一个问题:
如何合理地估算线程池大小?
怎么样设置 数量,执行效率最高?
怎么样设置 内存消耗最低?
是不是设置 线程池数量越大越好?
多线程 是不是不一定比单线程快?( 某些场景单线程确实比多线程快)
CPU资源是有限的,多线程 并发 都是在 抢占CPU资源,线程越多 抢占越激烈,单个线程获取资源的成本变高,执行完成时间会增加;
第一次 使用newFixedThreadPool 我设置了 线程数为80;
在运行中发现,往池子中 添加大量任务,第一个任务开始执行的时间点 特别晚,
而且 内存飙的特别高,究其原因是 他要把80个线程创建了,然后开始执行任务。
每个线程占用至少好几M甚至10几M的内存。
线程多了以后 内存就持续上涨;
朋友建议 使用Executors.newCachedThreadPool();
测试发现 当线程数 添加到 200+的时候 会造成内存溢出;
所以 直接 pass掉这个方式;
以下 是我 测试 Executors.newFixedThreadPool(NTHREADS) 方式 设置不同 线程数 的执行结果:
统一设置任务数 100;相当于并发100 已经满足大多数场景了;
newFixedThreadPool =1 总耗时46251毫秒,其中日志耗时17毫秒
newFixedThreadPool =2 总耗时25870毫秒,其中日志耗时11毫秒
newFixedThreadPool =4 总耗时18424毫秒,其中日志耗时21毫秒
newFixedThreadPool =5 总耗时18447毫秒,其中日志耗时5毫秒
newFixedThreadPool =8 总耗时16807毫秒,其中日志耗时9毫秒
newFixedThreadPool =9 总耗时17946毫秒,其中日志耗时8毫秒
newFixedThreadPool =16 总耗时17201毫秒,其中日志耗时9毫秒
newFixedThreadPool =32 总耗时17661毫秒,其中日志耗时808毫秒
newFixedThreadPool =48 总耗时18277毫秒,其中日志耗时25毫秒
newFixedThreadPool =64 总耗时18000毫秒,其中日志耗时2812毫秒
newCachedThreadPool 总耗时20374毫秒,其中日志耗时3毫秒
得出这个结果之后,去百度了一下 发现了这篇文章:
http://ifeve.com/how-to-calculate-threadpool-size/
结论是:
如果是CPU密集型应用,则线程池大小设置为N+1
如果是IO密集型应用,则线程池大小设置为2N+1
测试代码如下:
- package cn.jdy.tools;
-
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
-
-
-
-
-
-
-
- public class TestThreadPool {
- private static final int NTHREADS = 9;
- private static final ExecutorService exec = Executors.newFixedThreadPool(NTHREADS);
-
-
- static long tempcount = System.currentTimeMillis()/1000;
- static int prenum = 200;
- static int nums = 200;
- static int uses = 0;
-
- static long allstart = System.currentTimeMillis();
- static long logstime = 0;
-
- public static void main(String[] args) {
-
-
-
-
- for (int i = 0; i < nums; i++) {
- Runnable task = new Runnable() {
- @Override
- public void run() {
- try {
- long start = System.currentTimeMillis();
- task();
- long use = System.currentTimeMillis()-start;
-
- getCurrentThreads(use);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
-
-
- public void task(){
- try {
-
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- };
- exec.execute(task);
- }
- }
-
-
-
-
-
- public static void getCurrentThreads(long use){
-
-
-
- long start = System.currentTimeMillis();
-
- prenum = prenum-1;
- uses += use;
-
- long now = System.currentTimeMillis()/1000;
- if(now > tempcount){
- long freeMemory=Runtime.getRuntime().freeMemory() / 1024 / 1024;
- long totalMemory=Runtime.getRuntime().totalMemory() / 1024 / 1024;
- int cpu = Runtime.getRuntime().availableProcessors();
-
- System.out.printf("第%s秒: ", now-allstart/1000);
- int ts = (nums-prenum);
- System.out.printf("每秒处理数:%s ", ts);
- System.out.printf("平均耗时:%s ", ts==0?0:uses/ts);
- System.out.printf("进度:%s ", nums);
- System.out.printf("剩余:%s毫秒 ", nums*ts);
- System.out.printf("可用内存:%sm ", freeMemory);
- System.out.printf("可用总内存:%sm \n", totalMemory);
- tempcount = now;
- nums = prenum;
- uses =0;
- }
-
- logstime += System.currentTimeMillis()-start;
-
-
- if(prenum==0){
- long alluse = System.currentTimeMillis()-allstart;
- System.out.printf("总耗时%s毫秒,其中日志耗时%s毫秒\n",alluse,logstime);
- System.exit(0);
- }
-
- }
-
- }