基本原理源码
要理解原理,仍然要从源码看起。Java8的线程池【ThreadPoolExecutor】构造方法如下,
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
对构造参数要有一定的了解,才能明白线程池最为基本的组装。源码中的注释也写的很明白了,这里描述一下各个参数的含义和基本用法。
corePoolSize,核心线程数,用于执行任务的线程数量
maximumPoolSize,最大线程数,用于执行任务的最大线程数量
keepAliveTime,非核心线程的存活时长,线程存活控制
unit,非核心线程的存活时长单位,线程存活控制
workQueue,阻塞队列,任务存放的内存队列
threadFactory,线程池创建工厂
handler,拒绝策略,任务无法执行时,由该参数进行处理没有执行过的任务
接下来,观察执行任务的源码,来剖析其执行过程
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
/*
* Proceed in 3 steps:
*
* 1. If fewer than corePoolSize threads are running, try to
* start a new thread with the given command as its first
* task. The call to addWorker atomically checks runState and
* workerCount, and so prevents false alarms that would add
* threads when it shouldn't, by returning false.
*
* 2. If a task can be successfully queued, then we still need
* to double-check whether we should have added a thread
* (because existing ones died since last checking) or that
* the pool shut down since entry into this method. So we
* recheck state and if necessary roll back the enqueuing if
* stopped, or start a new thread if there are none.
*
* 3. If we cannot queue task, then we try to add a new
* thread. If it fails, we know we are shut down or saturated
* and so reject the task.
*/
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
if (! isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
else if (!addWorker(command, false))
reject(command);
}
同样的,根据注释和源码的解读,线程池执行任务的原理步骤如下:
1. 判断线程数是否小于【核心线程数】
2. 是,添加线程,执行该任务
3. 否,或者【2】添加失败,说明【核心线程数已满】,往下走
4. 线程池是否运行中,且能够将任务丢入【队列】
5. 判断一,二次确认,如果【线程池关闭】了,且能够从【队列】中弹出该任务,就执行【拒绝策略】
6. 当判断一不成立时,说明线程池还在运行,进入判断二,【核心线程数是否为0】,如果是就创建一个空闲的【非核心线程】,用于执行【队列】中的任务
7. 【4】否,说明队列已满,判断能否创建【非核心线程】,并传入该任务,如果不能,说明【非核心线程】也创建已满,进入【拒绝策略】
总结
线程池有以下几个特点,在实际应用上有很多帮助,也可以设计成很多更高层级的东西,比如数据库连接池、Hystrix 限流原理、调度管理等等。
- 降低资源消耗:通过重复利用已创建的线程降低线程创建和销毁造成的消耗
- 提高响应速度:当任务到达时,任务可以不需要等到线程创建就能立即执行
- 提高线程的可管理性。线程是稀缺资源,如果无限制地创建,不仅会消耗系统资源,还会降低系统的稳定性,使用线程池可以进行统一分配、调优和监控。