• 多线程——newCachedThreadPool线程池


    newCachedThreadPool线程池:

    理解:
      1).newCachedThreadPool可以创建一个无限大小的线程池(实际上是一个可缓存线程池)。
          可以通过Executors的静态方法创建线程池:
            public static ExecutorService newCachedThreadPool() 或者
             public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) 。
      2).返回的是ExecutorService对象线程池。
      3).newCachedThreadPool在执行过程中通常会创建与所需数量相同的线程,然后在它回收旧线程时停止创建新线程,因此它是合理的Executor的首选。
         只有当这种方式会引发问题时(内存溢出),你才需要切换到newFixedThreadPool()。
      4).无限制不代表不可以复用线程,假设说一开始进来10个任务,启动了10个线程,10个任务结束后,然后又来了5个任务(线程还没被回收),
          这时候会复用之前的线程,不会新起线程,这就达到了使用“线程池”的意义。
    使用场景:
      1. 耗时较短的任务。
      2. 任务处理速度 > 任务提交速度 ,这样才能保证不会不断创建新的进程,避免内存被占满。
    //首先看Executors中通过newCachedThreadPool创建线程池的方法的源码
    //此方法是通过new一个ThreadPoolExecutor来创建的
    //这个构造方法有5个参数:
    //第一个参数corePoolSize(核心池大小)为0,
    //第二个参数maximumPoolSize(最大线程池大小)为Integer.MAX_VALUE无限大,源码中是:public static final int   MAX_VALUE = 0x7fffffff;
    //第三个参数keepAliveTime(线程存货的时间)是60秒,意味着线程空闲时间超过60秒就会被杀死。
    //第五个参数采用SynchronousQueue装等待的任务,这个阻塞队列没有存储空间,这意味着只要有请求到来,就必须要找到一条工作线程处理他,
    //    如果当前没有空闲的线程,那么就会再创建一条新的线程。
    public class Executors {   
        public static ExecutorService newCachedThreadPool() {
            return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                          60L, TimeUnit.SECONDS,
                                          new SynchronousQueue<Runnable>());
        }
    }
    
    //ThreadPoolExecutor类中此构造函数源码:
    public class ThreadPoolExecutor extends AbstractExecutorService {
          public ThreadPoolExecutor(int corePoolSize,
                                  int maximumPoolSize,
                                  long keepAliveTime,
                                  TimeUnit unit,
                                  BlockingQueue<Runnable> workQueue) {
                this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
                     Executors.defaultThreadFactory(), defaultHandler);
        }
    }
    下面是Thinking in java书中的例子:
    public class LiftOff implements Runnable {
        protected int countDown = 5;
        private static int taskCount = 0;
        private final int id = taskCount++;
    
        public LiftOff() {
        }
    
        public LiftOff(int countDown) {
            this.countDown = countDown;
        }
    
        public String status() {
            return "# " + id + " ( " + (countDown > 0 ? countDown : "LiftOff !") + " )";
        }
    
        @Override
        public void run() {
            while (countDown-- > 0) {
                System.out.println(status());
                Thread.yield();
            }
        }
    }
    public class CachedThreadPool {
    
        public static void main(String[] args) {
            ExecutorService executorService = Executors.newCachedThreadPool();
            for (int i = 0; i < 3; i++) {
                executorService.execute(new LiftOff());
            }
        }
    }
    结果:
    # 2 ( 4 )
    # 0 ( 4 )
    # 1 ( 4 )
    # 2 ( 3 )
    # 0 ( 3 )
    # 2 ( 2 )
    # 1 ( 3 )
    # 2 ( 1 )
    # 0 ( 2 )
    # 2 ( LiftOff ! )
    # 1 ( 2 )
    # 0 ( 1 )
    # 1 ( 1 )
    # 0 ( LiftOff ! )
    # 1 ( LiftOff ! )
     
  • 相关阅读:
    Weebly轻松创建个人网站
    人生如游戏
    1 欧拉角与四元数计算机动画笔记
    图形学相关的一些数学基础书
    1047 Student List for Course (25 分)
    1124 Raffle for Weibo Followers (20 分)
    1065 A+B and C (64bit) (20 分)
    1036 Boys vs Girls (25 分)
    1039 Course List for Student (25 分)
    1054 The Dominant Color (20 分)
  • 原文地址:https://www.cnblogs.com/whx20100101/p/9862382.html
Copyright © 2020-2023  润新知