是什么
线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务。
解决问题
降低资源的消耗(重复利用)
提高响应速度 (并行执行)
提高线程的可关联性
组成部分
corePoolSize 线程池的基本大小
maximumPoolSize 线程池最大数量
keepAliveTime 线程活动保持的时间
taskQueue
ArrayBlockingQueue 基于数据数据结构的有界阻塞队列
LinkedBlockingQueue 链表的阻塞队列
SynchronousQueue 不存储元素的阻塞队列
PriorityBlockingQueue 优先级的无限队列
ThreadFactory 创建线程工厂
RejectExecutionHandler
AbortPolicy 直接抛异常
CallerRunsPolicy 只用调用者所在线程来运行任务
DiscardPolicy 丢弃队列里最近一个任务,并执行当前任务
DiscardOldestPolicy 不处理,丢失掉
如何使用
创建线程池
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @param threadFactory the factory to use when the executor
* creates a new thread
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
* @throws IllegalArgumentException if one of the following holds:
* {@code corePoolSize < 0}
* {@code keepAliveTime < 0}
* {@code maximumPoolSize <= 0}
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue}
* or {@code threadFactory} or {@code handler} is null
*/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
执行
public void execute(Runnable command) {
.....
}
工作流程
执行流程
转载《java并行编程的艺术》一张图(这张图非常详细)
提交一个新任务
判断核心线程池是否执行任务,如果不是,则创建新线程来执行任务
如果线程池都在执行,则判断队列是否满,如果没有满,则将任务放到队列中
如果队列满了,并且没有线程池线程个数小于最大个数,则创建新的线程执行任务
如果队列满了,并且也达到了线程池的最大个数,则走线程池的拒绝策略
其他
线程池的监控
目标:方便查看线程池的状态
参数
taskCount:线程池需要执行的任务数量
completedTaskCount:线程池在运行过程中已完成的任务数量
largestPoolSize线程池里曾经创建过最大的线程数量
getPoolSize 线程池的线程数量
getActiveCount 获取活动的线程数
自定义实现线程池监控
通过继承线程池来自定义线程池,然后重写beforeExecute、afterExecute、terminated方法
附
思维导图
参考资料
《java并行编程的艺术》