例子:

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. 分享知识-快乐自己:spring_Boot 中文返回给浏览器乱码 解析成问号?? fastJson jackJson

    心路历程: 在Controller中return 对象的时候,对象中的属性值中文部分在浏览器中 显示为问号?? 然后结果是这样的:?? 尝试排查原因: 中文乱码常有以下三种: 1.request.re ...

  2. Linus Torvalds: 成功的项目源于99%的汗水与1%的创新

    2017年2月15日,在加利福尼亚州的开源领袖峰会上,由Linux基金会执行董事Jim Zemlin进行的一次采访中,Torvalds讨论了他如何管理Linux内核的开发以及他对工作的态度. Linu ...

  3. Ajax处理后台返回的Json数据

    // 处理后台传来的Json字符串装换成Json对象 var dataJson = JSON.parse(data); // 此时可以从Json对象中取值 if(dataJson.result == ...

  4. Go丨语言学习笔记--func

    Java语言跟Go语言的函数比较 Go语言 func funcName(input type1,input type2,......)(output type1,output type2,...... ...

  5. 详解 pthread_detach()函数

    pthread_t 类型定义: typedef unsigned long int pthread_t; //come from /usr/include/bits/pthread.h 用途:pthr ...

  6. linux网络编程 ntohs, ntohl, htons,htonl inet_aton等详解

    ntohs =net to host short int 16位 htons=host to net short int 16位 ntohs =net to host long int 32位 hto ...

  7. 【Lintcode】095.Validate Binary Search Tree

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  8. 主库报 Error 12154 received logging on to the standby PING[ARC2]

    主备网络配置存在问题 一系列报错 [root@node1 bin]# ./srvctl  start database -d devdbPRCR-1079 : Failed to start reso ...

  9. cat的用法总结

    1 查看文件在LINUX下一切皆文件,光看见文件名和目录名对我们来说,还远远不够.今天,就来介绍一下可以打开文件的命令cat.当然,二进制的可执行文件,不能用cat. 在CentOS7下,以/etc/ ...

  10. <正则吃饺子> :关于redis配置文件参数详解

    来源于网络博文,感谢作者的分享,转载只为学习,方便查找,原文地址:http://blog.csdn.net/ljl890705/article/details/51540427 Redis是一个应用非 ...