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 ! )
 

最新文章

  1. Bringing Whoops Back to Laravel 5
  2. Web API项目中使用Area对业务进行分类管理
  3. 基于NXBRE规则引擎实现的柔性折扣策略
  4. C#按需序列化对象为Json字符串
  5. 调用python 报R6034 错误
  6. storm 分组
  7. HDU5086——Revenge of Segment Tree(BestCoder Round #16)
  8. js的基本概念详解
  9. 结构体 lock_sys
  10. PHP学习笔记——PHP脚本和JAVA连接mysql数据库
  11. Http 缓存
  12. servlet简介
  13. IE下判断IE版本语法使用
  14. SpringMVC + Mybatis bug调试 SQL正确,查数据库却返回NULL
  15. Azure Automation (6) 执行Azure SQL Job
  16. mysql命名规范
  17. 标签EL和JSTL解读
  18. es6入门4--promise详解
  19. codeforces471B
  20. 树剖||树链剖分||线段树||BZOJ4034||Luogu3178||[HAOI2015]树上操作

热门文章

  1. Don&#39;t forget to Cherish
  2. Matlab 瑞利信道仿真
  3. 正则表达式中,[\s\S]* 什么意思
  4. 美团店铺评价语言处理以及分类(tfidf,SVM,决策树,随机森林,Knn,ensemble)
  5. day_11 py 名片管理系统
  6. golang - interface的作用
  7. Maven Web项目部署到Tomcat下问题
  8. d9
  9. Winter-Camp欠债记录
  10. zabbix客户端自动注册