PrioritizedEsThreadPoolExecutor优先级线程池,顾名思义,根据线程任务的优先级来提供线程,每次提供优先级最高的线程任务,还没看实现前我们可以来思考一下如何做到,普通线程池提供的是线程队列,那么每次仅需要得到优先级最高的线程任务,那么可以改成使用优先队列,优先队列本质不过是堆。
先看下PrioritizedEsThreadPoolExecutor的构造
PrioritizedEsThreadPoolExecutor(String name, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
ThreadFactory threadFactory, ThreadContext contextHolder, ScheduledExecutorService timer) {
super(name, corePoolSize, maximumPoolSize, keepAliveTime, unit, new PriorityBlockingQueue<>(), threadFactory, contextHolder);
this.timer = timer;
}
这里可以看到直接调用了父类EsThreadPoolExecutor的构造,但是队列这里采用的是PriorityBlockingQueue队列。
EsThreadPoolExecutor(String name, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, ThreadContext contextHolder) {
this(name, corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, new EsAbortPolicy(), contextHolder);
}
EsThreadPoolExecutor继承自ThreadPoolExecutor,已经到jdk层面,这里无非将参数传递。
看其execute()方法
public void execute(Runnable command, final TimeValue timeout, final Runnable timeoutCallback) {
command = wrapRunnable(command);
doExecute(command);
if (timeout.nanos() >= 0) {
if (command instanceof TieBreakingPrioritizedRunnable) {
((TieBreak