目录
一、构造函数
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
public ScheduledThreadPoolExecutor(int corePoolSize,
ThreadFactory threadFactory) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue(), threadFactory);
}
public ScheduledThreadPoolExecutor(int corePoolSize,
RejectedExecutionHandler handler) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue(), handler);
}
public ScheduledThreadPoolExecutor(int corePoolSize,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue(), threadFactory, handler);
}
从构造函数中我们可以得出以下两点:
(1)非核心线程数是Integer.MAX_VALUE;活跃时间是0,也就是说线程执行完毕就被销毁
(2)等待队列是DelayedWorkQueue,无界队列,也就说明非核心线程和活跃时间的设置就没有意义了,因为队列永远不会被放满。
二、schedule方法
public <V> ScheduledFuture<V> schedule(Callable<V> callable,
long delay,
TimeUnit unit) {
if (callable == null || unit == null)
throw new NullPointerException();
RunnableScheduledFuture<V> t = decorateTask(callable,
new ScheduledFutureTask<V>(callable,
triggerTime(delay, unit)));
delayedExecute(t);
return t;
}
(1)RunnableScheduledFuture
1)RunnableScheduledFuture 继承 RunnableFuture + ScheduledFuture
RunnableFuture在TPE中已经聊过了。
2)ScheduledFuture 继承 Delayed + Future
RunnableScheduledFuture定义了一个是否周期执行的方法;
Delayed定义了一个获取延迟时间的方法。
(2)ScheduledFutureTask
构造函数,带period是周期执行的,不带period是一次性延迟执行的任务。
schedule方法调用的是无周期时间的构造函数
// 构造一个定时任务,在ns后执行,执行完成后结果为result
ScheduledFutureTask(Runnable r, V result, long ns) {
super(r, result);
this.time = ns;
this.period = 0;
this.sequenceNumber = sequencer.getAndIncrement();
}
// 构造一个周期任务,在ns后执行,每隔period执行一次,执行结果为result
ScheduledFutureTask(Runnable r, V result, long ns, long period) {
super(r, result);
this.time = ns;
this.period = period;
this.sequenceNumber = sequencer.getAndIncrement();
}
// 构造一个Callable定时任务,在ns后执行
ScheduledFutureTask(Callable<V> callable, long ns) {
super(callable);
this.time = ns;
this.period = 0;
this.sequenceNumber = sequencer.getAndIncrement();
}
(3)decorateTask
钩子函数,留给子类扩展
(4)delayedExecute
private void delayedExecute(RunnableScheduledFuture<?> task) {
if (isShutdown())
reject(task);
else {
super.getQueue().add(task);
if (isShutdown() &&
!canRunInCurrentRunState(task.isPeriodic()) &&
remove(task))
task.cancel(false);
else
ensurePrestart();
}
}
1)如果线程池关闭,执行拒绝策略;否则,继续
2)将当前任务添加到DelayedWorkQueue中
3)再次判断线程池是否关闭,如果关闭则判断当前状态是否能运行这个任务,不能运行则从DelayedWorkQueue中移除当前任务,然后取消当前任务
4)如果线程池没有关闭
void ensurePrestart() {
int wc = workerCountOf(ctl.get());
if (wc < corePoolSize)
addWorker(null, true);
else if (wc == 0)
addWorker(null, false);
}
获取当前工作线程数,如果小于核心线程数,启动一个核心线程,如果当前工作线程数等于0(只有当核心线程为0时,会执行),启动一个非核心线程。也就是保证至少有一个线程执行当前任务。
到目前为止,我们已经把当前任务放到延迟队列中了,同时保证了至少有一个线程是启动着的,但是线程是怎样执行的,我们并不知道。那么是在哪里执行的,addWorker是TPE中的方法,肯定不是这里,所以执行任务的方法就是schedule中new ScheduledFutureTask时ScheduledFutureTask类的run方法。
任务已经放到队列中了,现在是run执行,而这是一个延迟执行的定时任务,肯定不能线程一启动就执行(没到延迟时间肯定不能执行),那处理这个延迟周期执行的是谁呢?是addWorker吗?肯定不是。那会是谁,就是DelayedWorkQueue(因为线程启动会调用WorkQueue的take方法取任务<TPE的getTask方法>),所以自然就是DelayedWorkQueue的take方法取的任务。
public RunnableScheduledFuture<?> take() throws InterruptedException {
// 多线程操作,所以需要上锁
final ReentrantLock lock = this.lock;
// 同时这把锁是 可响应中断的方式进行加锁
lock.lockInterruptibly();
try {
for (;;) {
// 取出第一个任务
RunnableScheduledFuture<?> first = queue[0];
// 如果第一个任务为空,表示队列中无任务,则等待
if (first == null)
available.await();
// 不为空
else {
// 取这个任务的超时时间(单位ns)
long delay = first.getDelay(NANOSECONDS);
// 如果超时时间小于等于0,代表任务已经超时,返回当前任务
// 即返回到TPE的getTask中,作为getTask的返回值返回到runWorker中
if (delay <= 0)
return finishPoll(first);
// 如果没有超时,不持有当前任务
first = null; // don't retain ref while waiting
// 如果leader不为空,表示第二个线程开始执行了,此线程直接await
// 为啥要有一个leader线程,假如没有leader,有10个线程等待一个
// 任务,当任务开始执行时,会唤醒10个线程,而这完全没有必要,只
// 需要让一个线程拿到此任务去执行即可,因为此时把其他9个线程唤
// 醒后并不会执行此任务,很浪费资源(只有一个任务需要执行,你为
// 啥要唤醒所有的线程呢,所以需要一个leader)
if (leader != null)
available.await();
// 如果leader是空,把当前线程赋给leader
else {
Thread thisThread = Thread.currentThread();
leader = thisThread;
try {
// leader赋值完毕,等待对应的延迟时间
available.awaitNanos(delay);
} finally {
// 等待完毕后leader已经去执行任务了,所以leader置空
if (leader == thisThread)
leader = null;
}
}
}
}
} finally {
// 此时就有一个问题,当leader等待完毕后去执行任务了,那么其他等待的线程
// 谁来唤醒呢,所以才有了这里的逻辑
// 当队列中还有任务时,则唤醒一个等待的线程
if (leader == null && queue[0] != null)
available.signal();
lock.unlock();
}
}
此时还有一个问题,当任务执行完毕时,如果是周期执行的任务,还得重新计算下次执行的时间,所以就回到了ScheduledFutureTask类的run方法
public void run() {
boolean periodic = isPeriodic();
// 如果当前状态不能执行这个任务,则取消此任务
if (!canRunInCurrentRunState(periodic))
cancel(false);
// 如果能执行,且此任务不是周期性执行的任务,直接run
else if (!periodic)
ScheduledFutureTask.super.run();
// 如果是周期性执行的任务,则当前线程执行任务,且将当前线程状态还原为NEW的状态
// 为啥要重新设置,因为是周期执行的任务,此次执行完毕后重新将其放到队列中等待
// 下次执行,同时恢复状态。
else if (ScheduledFutureTask.super.runAndReset()) {
// 设置下次执行时间
setNextRunTime();
// 将任务放到队列中,放的时候反复判断当前状态是否能执行这个任务,防止
// 调用了shutdown或shutdownNow方法
reExecutePeriodic(outerTask);
}
}
void reExecutePeriodic(RunnableScheduledFuture<?> task) {
if (canRunInCurrentRunState(true)) {
// 将此任务放到队列中,放的前后反复判断当前状态是否能执行这个任务,防止
// 调用了shutdown或shutdownNow方法
super.getQueue().add(task);
// 当前状态不能执行这个任务,则从队列中移除,取消当前任务
if (!canRunInCurrentRunState(true) && remove(task))
task.cancel(false);
else
// 放成功了,且当前状态能执行此任务,则准备下一次执行
// ensurePrestart方法确保至少有一个线程在下一次执行任务
ensurePrestart();
}
}
reExecutePeriodic(outerTask),这个outerTask,就是在scheduleAtFixedRate和scheduleWithFixedDelay中设置进去的,也就是在scheduleAtFixedRate和scheduleWithFixedDelay中第一次执行ScheduledFutureTask这个任务,然后再执行封装的这个任务(decorateTask方法封装任务,而这个方法是一个钩子函数,所以这里是一个扩展点),这个outerTask就是封装的任务。
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit) {
if (command == null || unit == null)
throw new NullPointerException();
if (period <= 0)
throw new IllegalArgumentException();
// 第一次执行ScheduledFutureTask任务
ScheduledFutureTask<Void> sft =
new ScheduledFutureTask<Void>(command,
null,
triggerTime(initialDelay, unit),
unit.toNanos(period));
// 后面执行封装的这个任务
RunnableScheduledFuture<Void> t = decorateTask(command, sft);
sft.outerTask = t;
delayedExecute(t);
return t;
}
OK,关于ScheduledThreadPoolExecutor就讲这么多,可能有小伙伴会说scheduleAtFixedRate和scheduleWithFixedDelay方法咋没聊呢,相信认真看完并理解的小伙伴应该是不会有这个疑问的。