什么是饥饿的线程池
线程池运行的基本逻辑是C->Q->M,有没有办法把执行逻辑改成C->M->Q呢?
这种线程池可以比喻成饥饿的线程池,因为不管提交多少任务,都是尽其所能的先分配线程执行,尽快消费。
这种线程池可以提高处理速度和效率,特别适合用在非CPU密集型的处理场景下延迟更低响应更快,提高吞吐量,而且空闲时还不占用max线程。
例如:
Tomcat处理接收请求的200个线程池
org.apache.tomcat.util.threads.ThreadPoolExecutor
Dubbo中也提供有这种线程池的SPI实现
org.apache.dubbo.common.threadpool.support.eager.EagerThreadPoolExecutor
(Eager急切的)
实现原理
以org.apache.dubbo.common.threadpool.support.eager.EagerThreadPoolExecutor为例,实现比较简单清晰
1、正常的ThreadPoolExecutor的执行逻辑是
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();
// 1、线程数量小于core创建线程执行
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
// 2、线程数量大于core放入queue
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);
}
// 3、队列也满了创建线程执行,ThreadPoolExecutor#addWorker第二个参数true代表使用corePoolSize作为绑定,否则使用maximumPoolSize
else if (!addWorker(command, false))
reject(command);
}
2、EagerThreadPoolExecutor是从队列下手,配合TaskQueue来欺骗JDK的ThreadPoolExecutor
// TaskQueue改写了LinkedBlockingQueue的offer方法
@Override
public boolean offer(Runnable runnable) {
if (executor == null) {
throw new RejectedExecutionException("The task queue does not have executor!");
}
// 如果有空闲线程还是放队列
int currentPoolThreadSize = executor.getPoolSize();
// have free worker. put task into queue to let the worker deal with task.
if (executor.getActiveCount() < currentPoolThreadSize) {
return super.offer(runnable);
}
// 如果线程数小于Max则直接拒绝添加到队列,欺骗JDK的ThreadPoolExecutor以为队列满了去创建线程到达Max
// return false to let executor create new worker.
if (currentPoolThreadSize < executor.getMaximumPoolSize()) {
return false;
}
// 真正的添加到队列
// currentPoolThreadSize >= max
return super.offer(runnable);
}
但是这样改写queue会导致一个副作用就是在offer内return false后,addWorker(command, false)时此时线程数达到了Max,那么线程池就会拒绝这个任务,所以还需要
3、EagerThreadPoolExecutor#execute中处理TaskQueue带来的副作用
@Override
public void execute(Runnable command) {
if (command == null) {
throw new NullPointerException();
}
try {
super.execute(command);
} catch (RejectedExecutionException rx) {
// 此时抛拒绝任务可能是副作用导致的,所以还需要检查下queue是否真的满了,retryOffer是JDK原生的添加到队列中的方法
// retry to offer the task into queue.
final TaskQueue queue = (TaskQueue) super.getQueue();
try {
if (!queue.retryOffer(command, 0, TimeUnit.MILLISECONDS)) {
throw new RejectedExecutionException("Queue capacity is full.", rx);
}
} catch (InterruptedException x) {
throw new RejectedExecutionException(x);
}
}
}