前言

定时器线程池提供了定时执行任务的能力,即可以延迟执行,可以周期性执行。但定时器线程池也还是线程池,最底层实现还是ThreadPoolExecutor,可以参考我的另外一篇文章多线程–精通ThreadPoolExecutor。

特点说明

1.构造函数

public ScheduledThreadPoolExecutor(int corePoolSize) {
// 对于其他几个参数在ThreadPoolExecutor中都已经详细分析过了,所以这里,将不再展开
// 这里我们可以看到调用基类中的方法时有个特殊的入参DelayedWorkQueue。
// 同时我们也可以发现这里并没有设置延迟时间、周期等参数入口。
// 所以定时执行的实现必然在DelayedWorkQueue这个对象中了。
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}

2.DelayedWorkQueue

DelayedWorkQueue是在ScheduledThreadPoolExecutor的一个内部类,实现了BlockingQueue接口

里面存放任务队列的数组如下:

private RunnableScheduledFuture>[] queue =
new RunnableScheduledFuture>[INITIAL_CAPACITY];

我们分析过ThreadPoolExecutor,它从任务队列中获取任务的方式为poll和take两种,所以看一下poll和take两个方法的源码,回顾一下,ThreadPoolExecutor它会调用poll或take方法,先poll,再take,只要其中一个接口有返回就行

public RunnableScheduledFuture> poll() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
RunnableScheduledFuture> first = queue[0];
// 这里有个getDelay,这是关键点,获取执行延时时间
// 但是如果我们有延时设置的话,这就返回空了,然后就会调用take方法
if (first == null || first.getDelay(NANOSECONDS) > 0)
return null;
else
return finishPoll(first);
} finally {
lock.unlock();
}
}
public RunnableScheduledFuture> take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
for (;;) {
RunnableScheduledFuture> first = queue[0];
if (first == null)
available.await();
else {
// 获取延时时间
long delay = first.getDelay(NANOSECONDS);
if (delay <= 0)
return finishPoll(first);
first = null; // don't retain ref while waiting
if (leader != null)
available.await();
else {
Thread thisThread = Thread.currentThread();
leader = thisThread;
try {
// 使用锁,执行延时等待。
// 使用锁,执行延时等待。
// 使用锁,执行延时等待。
available.awaitNanos(delay);
} finally {
if (leader == thisThread)
leader = null;
}
}
}
}
} finally {
if (leader == null && queue[0] != null)
available.signal();
lock.unlock();
}
}