文章目录
前言
《阿里巴巴java开发手册》中强制要求:线程池不允许使用Executors去创建,而是通过ThreadPoolExecutor的方式,这样的处理方式让写的同学更加明确线程池的运行规则,规避资源耗尽的风险。
参考博客(必看):java线程池ThreadPoolExecutor类使用详解
一、为什么要通过ThreadPoolExecutor方式获取线程池?
- 《阿里巴巴java开发手册》中给出的答案如下:
说明:Executors返回的线程池对象的弊端如下:
1) FixedThreadPool和SingleThreadPool: 允许的请求队列长度为 Integer.MAX_VALUE,可能会堆积大量的请求,从而导致 OOM。
2) CachedThreadPool: 允许的创建线程数量为 Integer.MAX_VALUE,可能会创建大量的线程,从而导致 OOM。
- 分析Executors获取线程池的源码,发现底层也是通过ThreadPoolExecutor获取线程池的。
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
看到这里,大家应该明白为什么开发手册中不允许使用Executors去创建线程池了吧。
二、通过ThreadPoolExecutor获取线程池的使用示例
1. 代码示例
public class ThreadPoolTest {
public static void main(String[] args) {
System.out.println("开始时间: " + System.currentTimeMillis());
// 0.创建线程池
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 50L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
// 1.Runnable方式创建任务
Runnable command = new Runnable() {
@Override
public void run() {
System.out.println("runnable: " + Thread.currentThread().getName() + " " + System.currentTimeMillis());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
// 执行Runnable任务
for (int i = 0; i < 10; i++) {
threadPoolExecutor.execute(command);
}
// 2.Callable方式创建任务
Callable<String> callCommand = () -> {
String s = "callable: " + Thread.currentThread().getName() + " " + System.currentTimeMillis();
Thread.sleep(300);
return s;
};
// 创建Callable任务异步调用结果的容器
ArrayList<Future> futures = new ArrayList<>(10);
// 执行Callable任务
for (int i = 0; i < 10; i++) {
Future<String> callResult = threadPoolExecutor.submit(callCommand);
futures.add(callResult);
}
// 获取Callable任务异步调用的结果
for (Future future : futures) {
try {
System.out.println(future.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
System.out.println("结束时间: " + System.currentTimeMillis());
System.exit(0);
}
}
2. 运行结果
开始时间: 1613821067943
runnable: pool-1-thread-1 1613821067949
runnable: pool-1-thread-2 1613821067949
runnable: pool-1-thread-3 1613821067949
runnable: pool-1-thread-4 1613821067950
runnable: pool-1-thread-5 1613821067950
runnable: pool-1-thread-7 1613821068318
runnable: pool-1-thread-6 1613821068318
runnable: pool-1-thread-8 1613821068319
runnable: pool-1-thread-10 1613821068319
runnable: pool-1-thread-9 1613821068319
callable: pool-1-thread-1 1613821068950
callable: pool-1-thread-2 1613821068950
callable: pool-1-thread-5 1613821068951
callable: pool-1-thread-4 1613821068951
callable: pool-1-thread-3 1613821068951
callable: pool-1-thread-6 1613821068018
callable: pool-1-thread-7 1613821068018
callable: pool-1-thread-8 1613821068019
callable: pool-1-thread-9 1613821068019
callable: pool-1-thread-10 1613821068019
结束时间: 1613821069251
3. 结果分析(精髓)
通过打印结果,可以发现其调用规律,下面以时间戳为时间线分析一波:
- 1613821067943:开始时间点
- 1613821067943 - 1613821067949:[6ms] 初始化线程池
- 1613821067949:前5个runnable任务分配给线程池中的5个线程去执行(pool-1-thread-1 ~ pool-1-thread-5)
- 1613821067950 - 1613821068018:[68ms] 后5个runnable任务进入了workQueue等待执行;前5个callable任务也进入了workQueue等待执行,此时workQueue的数量达到最大容量10
- 1613821068018:由于workQueue已满,但线程池中的线程数量未达到最大值10,所以后5个callable任务分配给线程池中新增的5个线程去执行(pool-1-thread-6 ~ pool-1-thread-10)
- 1613821068018 - 1613821068318:[300ms] 后5个callable任务进行执行
- 1613821068319:后5个callable任务执行完毕,pool-1-thread-6 ~ pool-1-thread-10这5个线程空闲出来,并从workQueue中取前5个待执行的任务进行执行(即runnable任务的后5个)
- 1613821067950 - 1613821068950:[1000ms] 前5个runnable任务进行执行
- 1613821068950:前5个runnable任务执行完毕,pool-1-thread-1 ~ pool-1-thread-5这5个线程空闲出来,并从workQueue中取剩下的5个待执行的callable任务进行执行
- 1613821068951 - 1613821069251:[300ms] 前5个callable任务进行执行
- 1613821069251:前5个callable任务执行完毕(注意:主线程到这里用
System.exit(0);
命令提前结束了程序) - 1613821068319 - 1613821069319:[1000ms] 后5个runnable任务进行执行(可见
System.exit(0);
提前结束了还没有执行完的线程任务)