什么是线程池?

线程池:一种基于池化思想管理和使用线程的机制
ExecutorService接口:进行线程池的操作访问Executors类:创建线程池的工具类ThreadPoolExecutor及其子类:封装线程池的核心参数和运行机制线程池常见的方法:
void execute(Runnable command);Future submit(Callable task); void shutdown();或shutdownNow();boolean awaitTermination(long timeout,TimeUnit unit);
corePoolSize线程池核心线程数:可以理解为线程池维护的最小线程数,核心线程创建后不会被回收。大于核心线程数的吸纳成,在空闲时间超过keepAliveTime后会被回收
maximumPoolSize线程池最大线程数:线程池允许创建的最大线程数量(包含核心线程池数量)
keepAliveTime非核心线程存活时间:当一个可被回收的线程的空闲时间大于keepAliveTime就会被回收
TimeUnit时间单位:参数keepAliveTime的时间单位
BlockingQueue阻塞工作队列:用于存储等待执行的任务
ThreadFactory线程工厂:用于创建线程,以及自定义线程名称,需要实现ThreadFactory接口
RejectedExecutionHandler拒绝策略:当线程池中的线程耗尽,并且工作队列已满时,新提交的任务,将会启动拒绝策略处理
FixedThreadPool:线程数固定的线程池
通过Executors.newFixedThreadPool(n);方法创建
// 创建有十个线程的线程池
Executors.newFixedThreadPool(10);
// Executors.newFixedThreadPool(n)方法源码
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
通过源码,FixedThreadPool线程池的核心线程数和最大线程数都是传入的参数,所以是固定线程数的线程池;
CacheThreadPool:线程数根据任务动态调整的线程池
通过Executors.newCachedThreadPool();方法创建
// 创建一个CacheThreadPool线程池
Executors.newCachedThreadPool();
// 源码
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
通过源码,CacheThreadPool线程池的核心线程数为0,最大线程数为Integer的最大值,非核心线程的存活时间为60s
SingleThreadPool:仅提供一个单线程的线程池
通过Executors.*newSingleThreadExecutor*();方法创建
// 创建一个SingleThreadPool线程池
Executors.newSingleThreadExecutor();
// 源码
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
通过源码,SingleThreadPool线程池的核心线程数和最大线程数都为1,其他线程的存储时间为0
ScheduledThreadPool:能实现定时、周期性任务的线程池
通过Executors.newScheduledThreadPool(n);方法创建
// 创建一个ScheduledThreadPool线程池
Executors.newScheduledThreadPool(10);
// 源码
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
⬇⬇⬇⬇⬇⬇
// new ScheduledThreadPoolExecutor(corePoolSize);方法源码
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
通过源码,ScheduledThreadPool线程池的核心线程数为传入的参数,最大线程数为Integer类型的最大值,非核心线程的存活时间为0,空闲立马被回收