线程池(3)Executors.newCachedThreadPool
例子:
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的更多相关文章
- 线程池之 Executors
线程池之 Executors + 面试题 线程池的创建分为两种方式:ThreadPoolExecutor 和 Executors,上一节学习了 ThreadPoolExecutor 的使用方式,本节重 ...
- 线程池工厂Executors编程的艺术
Executors是一个线程池的工厂类,提供各种有用的线程池的创建,使用得当,将会使我们并发编程变得简单!今天就来聊聊这个工厂类的艺术吧! Executors只是Executor框架的主要成员组件之一 ...
- Java线程池ThreadPoolExecutor&&Executors
一.先看看传统的开启线程. new Thread(new Runnable() { @Override public void run() { } }).start(); 缺点: 1.每次new Th ...
- 线程池(2)-Executors提供4个线程池
1.为什么不使用Executors提供4个线程池创建线程池 阿里巴巴开放手册这样写: . [强制]线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式, ...
- Java并发包线程池之Executors、ExecutorCompletionService工具类
前言 前面介绍了Java并发包提供的三种线程池,它们用处各不相同,接下来介绍一些工具类,对这三种线程池的使用. Executors Executors是JDK1.5就开始存在是一个线程池工具类,它定义 ...
- Executors、ThreadPoolExecutor线程池讲解
官方+白话讲解Executors.ThreadPoolExecutor线程池使用 Executors:JDK给提供的线程工具类,静态方法构建线程池服务ExecutorService,也就是Thread ...
- Java并发编程:线程池的使用
Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...
- java多线程详解(7)-线程池的使用
在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, 这样频繁创建线程就会大大降低系 ...
- java并发编程(4)--线程池的使用
转载:http://www.cnblogs.com/dolphin0520/p/3932921.html 一. java中的ThreadPoolExecutor类 java.util.concurre ...
- 深入理解Java之线程池
原作者:海子 出处:http://www.cnblogs.com/dolphin0520/ 本文归作者海子和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则 ...
随机推荐
- IDEAL葵花宝典:java代码开发规范插件 lombok 插件
前言: lombok简介: lombok是暑假来到公司实习的时候发现的一个非常好用的小工具,刚见到的时候就感觉非常惊艳,有一种相见恨晚的感觉,用了一段时间之后感觉的确挺不错,所以特此来推荐一下. 那么 ...
- 一些flag
连续两道组合计数题 WA 在杨辉三角上,TM 我要是联赛杨辉三角萎了就剁*. 又连续两道题萎在数组越界上,TM 我要是联赛数组越界就吃*.
- CodeForces 547E:Mike and Friends(AC自动机+DFS序+主席树)
What-The-Fatherland is a strange country! All phone numbers there are strings consisting of lowercas ...
- 「LuoguP1429」 平面最近点对(加强版)
题目描述 给定平面上n个点,找出其中的一对点的距离,使得在这n个点的所有点对中,该距离为所有点对中最小的 输入输出格式 输入格式: 第一行:n:2≤n≤200000 接下来n行:每行两个实数:x y, ...
- ACM学习历程—ZOJ 3861 Valid Pattern Lock(dfs)
Description Pattern lock security is generally used in Android handsets instead of a password. The p ...
- Mysql常用命令行大全(一)
登录到mysql中,然后在mysql的提示符下运行下列命令,每个命令以分号结束. 1. 显示数据库列表. show databases; 缺省有两个数据库:mysql和test. mysql库存放着m ...
- 获取iOS应用中当前处于Activity状态的ViewController
转自:http://blog.csdn.net/lixuwen521/article/details/41447507 // 获取当前处于activity状态的view controller - (U ...
- Eclipse+Maven
http://www.cnblogs.com/alunchen/p/5632497.html
- SSH 整合时报内存溢出 缓存无法释放
简单点讲, 我的问题就是jar包一样, 但版本不一样, 还有重复的jar导入了, 导致了这个问题, 别人的我不知道, 问题知道了, 答案也就有了, 是重复加载的问题, 删掉重复的就好了.
- js取得数组重复元素
function duplicates(arr) { var result=[]; arr.sort();//进行排序,重复的都相邻了 ;i<arr.length;i++){ ]&&am ...