例子:

ExecutorService es = Executors.newCachedThreadPool();
try {
for (int i = 0; i < 20; i++) {
Runnable syncRunnable = new Runnable() {
@Override
public void run() {
log.info(Thread.currentThread().getName());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
es.execute(syncRunnable);
}
} finally {
es.shutdown();
}

运行结果:

            10:21:04.610 pool-1-thread-13
10:21:04.612 pool-1-thread-3
10:21:04.612 pool-1-thread-7
10:21:04.612 pool-1-thread-2
10:21:04.610 pool-1-thread-14
10:21:04.612 pool-1-thread-6
10:21:04.611 pool-1-thread-8
10:21:04.611 pool-1-thread-11
10:21:04.611 pool-1-thread-4
10:21:04.610 pool-1-thread-1
10:21:04.611 pool-1-thread-20
10:21:04.611 pool-1-thread-12
10:21:04.610 pool-1-thread-16
10:21:04.611 pool-1-thread-5
10:21:04.611 pool-1-thread-9
10:21:04.610 pool-1-thread-17
10:21:04.610 pool-1-thread-18
10:21:04.610 pool-1-thread-10
10:21:04.611 pool-1-thread-15
10:21:04.611 pool-1-thread-19

调用的调用的ThreadPoolExecutor:

public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}

corePoolSize=0,maximumPoolSize=Integer.MAX_VALUE

keepAliveTime=60秒

allowCoreThreadTimeout=false(默认)

因此,

  • 核心线程数为0
  • 每来一个任务,先查看缓冲池中是否有可用线程(没超过60秒的),如果有,则用;没有,则就创建一个新线程
  • 因为核心线程数为0,池中的线程当达到60秒时,会超时关闭,直到核心线程数=0

线程池(3)Executors.newCachedThreadPool的更多相关文章

  1. 线程池之 Executors

    线程池之 Executors + 面试题 线程池的创建分为两种方式:ThreadPoolExecutor 和 Executors,上一节学习了 ThreadPoolExecutor 的使用方式,本节重 ...

  2. 线程池工厂Executors编程的艺术

    Executors是一个线程池的工厂类,提供各种有用的线程池的创建,使用得当,将会使我们并发编程变得简单!今天就来聊聊这个工厂类的艺术吧! Executors只是Executor框架的主要成员组件之一 ...

  3. Java线程池ThreadPoolExecutor&&Executors

    一.先看看传统的开启线程. new Thread(new Runnable() { @Override public void run() { } }).start(); 缺点: 1.每次new Th ...

  4. 线程池(2)-Executors提供4个线程池

    1.为什么不使用Executors提供4个线程池创建线程池 阿里巴巴开放手册这样写: . [强制]线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式, ...

  5. Java并发包线程池之Executors、ExecutorCompletionService工具类

    前言 前面介绍了Java并发包提供的三种线程池,它们用处各不相同,接下来介绍一些工具类,对这三种线程池的使用. Executors Executors是JDK1.5就开始存在是一个线程池工具类,它定义 ...

  6. Executors、ThreadPoolExecutor线程池讲解

    官方+白话讲解Executors.ThreadPoolExecutor线程池使用 Executors:JDK给提供的线程工具类,静态方法构建线程池服务ExecutorService,也就是Thread ...

  7. Java并发编程:线程池的使用

    Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...

  8. java多线程详解(7)-线程池的使用

    在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, 这样频繁创建线程就会大大降低系 ...

  9. java并发编程(4)--线程池的使用

    转载:http://www.cnblogs.com/dolphin0520/p/3932921.html 一. java中的ThreadPoolExecutor类 java.util.concurre ...

  10. 深入理解Java之线程池

    原作者:海子 出处:http://www.cnblogs.com/dolphin0520/ 本文归作者海子和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则 ...

随机推荐

  1. html5--2.4新的布局元素(3)-section

    html5--2.4新的布局元素(3)-section 学习要点 了解section元素的语义和用法 通过实例理解section元素的用法 article元素和section元素的区别和共同点 art ...

  2. 基类的两个派生类再派生一个派生类 用virtual避免二义性

    class vehicle{ int MaxSpeed; int Weight;public: vehicle(int maxspeed, int weight) :MaxSpeed(maxspeed ...

  3. 「CF779B」「LOJ#10201.」「一本通 6.2 练习 4」Sherlock and His Girlfriend(埃氏筛

    题目描述 原题来自:Codeforces Round #400 B. Sherlock 有了一个新女友(这太不像他了!).情人节到了,他想送给女友一些珠宝当做礼物. 他买了 nnn 件珠宝.第 iii ...

  4. ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)

    Description We all use cell phone today. And we must be familiar with the intelligent English input ...

  5. 使用webdriver出现的问题:[18796:1808:0730/131103.313:ERROR:install_util.cc(600)] Failed to read HKLM\SOFTWARE\Policies\Google\Chrome\MachineLevelUserCloudPolicyEnrollmentToken: 系统找不到指定的文件。 (0x2) DevTools lis

    1.注册表导致 [5956:4996:0710/155156.898:ERROR:install_util.cc(589)] Unable to create registry key HKLM\SO ...

  6. Vue cli项目开启Gzip

    目录 安装 compression-webpack-plugin 更改配置文件 服务器开启gzip功能 安装 compression-webpack-plugin 建议安装v1.1.11版本,最新版本 ...

  7. Asp.Net 无法获取IIS拾取目录的解决办法[译]

    Asp.Net 无法获取IIS拾取目录的解决办法 作者:Jason Doucette  [MCP] 翻译:彭远志 原文地址:Fixing the cannot get IIS pickup direc ...

  8. C#支持从自定义日期时间格式到DateTime类型

            /// <summary>         ///         /// </summary>         /// <param name=&quo ...

  9. [pe531]Chinese leftovers

    题意:1e6~1e6+5000之间任意两个之间同余方程组的解.余数为欧拉函数. 解题关键:线性筛预处理,扩展中国剩余定理暴力求解. #include<cstdio> #include< ...

  10. 《精通Spring4.X企业应用开发实战》读后感第二章