一、什么是线程池?
线程池: 是指在初始化一个多线程应用程序过程中创建一个线程集合,然后在需要执行新的任务时尽可能重用这些线程而不是新建一个线程。
二、线程池种类介绍
1. 线程池种类介绍——Executors
1.1 newFixedThreadPool()
newFixedThreadPool():固定线程数的线程池
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
线程池特点:
- 核心线程数和最大线程数大小一样
- 没有所谓的非空闲时间,即keepAliveTime为0
- 阻塞队列为无界队列LinkedBlockingQueue
缺点
- 如果