关于Java线程池的解析与用法
关于Java线程池的解析与用法
inkOrCloud继承关系图
进程池的几种常用创建类型
通过ThreadPoolExecutor构造函数创建
1 | public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) |
主要参数
corePoolSize:最大核心线程数(必须)maximumPoolSize:线程池最大总线程数(必须)keepAliveTime:空闲线程的最长存活时间(必须)unit:时间单位(必须)workQueue:任务储存队列(必须)ThreadFactory:线程工厂(默认Executors.DefaultThreadFactory)handler:拒绝策略(默认AbortPolicy)
拒绝策略
拒绝策略是线程池无法接受新任务时的处理机制 1
2
3public interface RejectedExecutionHandler {
void rejectedExecution(Runnable var1, ThreadPoolExecutor var2);
}ThreadPoolExecutor.AbortPolicy:抛出一个RejectedExecutionException错误
1
2
3
4
5
6
7
8public static class AbortPolicy implements RejectedExecutionHandler {
public AbortPolicy() {
}
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() + " rejected from " + e.toString());
}
}ThreadPoolExecutor.DiscardOldestPolicy:丢弃队列中最早加入的任务
1
2
3
4
5
6
7
8
9
10
11
12public static class DiscardOldestPolicy implements RejectedExecutionHandler {
public DiscardOldestPolicy() {
}
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
e.getQueue().poll();
e.execute(r);
}
}
}ThreadExecutor.DiscardPolicy:丢弃当前任务,即不做任何操作
1
2
3
4
5
6
7public static class DiscardPolicy implements RejectedExecutionHandler {
public DiscardPolicy() {
}
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
}
}ThreadExecutor.CallerRunsPolicy:由提交任务的线程执行任务
1
2
3
4
5
6
7
8
9
10
11public static class CallerRunsPolicy implements RejectedExecutionHandler {
public CallerRunsPolicy() {
}
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
r.run();
}
}
}
通过Executors框架工厂函数创建
Executors.newFixedThreadPool:创建一个固定线程的线程1
2
3public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue());
}Executors.SingleThreadExecutor:创建一个单线程线程池1
2
3
4
5
6
7public static ExecutorService newSingleThreadExecutor() {
return newSingleThreadExecutor(defaultThreadFactory());
}
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
return new AutoShutdownDelegatedExecutorService(new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), threadFactory));
}Executors.newCachedThreadPool:创建一个无核心线程且线程上限为Integer.MAX_VALUE的线程池注:以上工厂函数都可在最后加上ThreadFactory对象参数自定义线程工厂且任务队列最大容量为1
2
3public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue());
}Integer.MAX_VALUE

![[leetcode 3321][Go][有序集合]计算子数组的 x-sum II](https://s3.inkorcloud.top/image/2025/11/c9f55b2ecb04d16a57547b9f4de294ef.png)
![[leetcode 2589][Go][贪心]完成所有任务的最少时间](https://s3.inkorcloud.top/image/2025/11/b966fd1a91aa1ea098b86e6d778523c2.png)
![[leetcode 3234][Go]统计1显著的字符串的数量](https://s3.inkorcloud.top/image/2025/11/a72a47911de291f51cbb5902e4811c1f.png)
![[leetcode 1611][Go][位运算][记忆化搜索]使整数变为 0 的最少操作次数](https://s3.inkorcloud.top/image/2025/11/9715b5f3d8a7574ded3648767538558e.png)
![[leetcode 2528][Go][二分答案][差分数组][前缀和]最大化城市的最小电量](https://s3.inkorcloud.top/image/2025/11/fa0013722d9361b4383839722394dcf2.png)