ExecutorService最后一块是定时/周期执行任务的接口ScheduledExecutorService。这篇说说ScheduledExecutorService相关的内容。
ScheduledExecutorService接口扩展了ExecutorService接口,在ExecutorService的生命周期管理,异步执行任务,批量执行任务的基础上,扩展了定时/周期执行任务的能力。
1. schedule方法提供了Runnable和Callable的两种任务的输入,可以设置延迟时间来执行任务,是一次性的
2. scheduleAtFixedRate方法提供了固定周期执行任务的能力,只支持Runnable任务。需要注意的是一旦某次执行任务抛出异常,后续的任务将会停止执行。任务的执行时间是initialDelay,initialDelay+period, initialDelay + 2 * period,不管前一个任务是否执行完成。
3. scheduleWithFixedDelay方法提供了固定延迟时间执行任务的能力,只支持Runnable任务。后一天任务在前一个任务执行完成后,再延迟delay时间再执行。
public interface ScheduledExecutorService extends ExecutorService { public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit); public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit); public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit); public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit); }
public interface ScheduledFuture<V> extends Delayed, Future<V> { } public interface Delayed extends Comparable<Delayed> { long getDelay(TimeUnit unit); }
而为了把ScheduledFuture接口和Runnable接口适配,又定义了RunnableScheduledFuture接口扩展了RunnableFuture和ScheduleFuture,这样RunnableScheduledFuture就可以被Executor框架执行,又具备了Future异步状态控制和Scheduled定时周期执行的能力。
public interface RunnableScheduledFuture<V> extends RunnableFuture<V>, ScheduledFuture<V> { boolean isPeriodic(); }
ScheduledFutureTask的主要属性如下
1. private final long sequenceNumber; 序列号
2. private long time; 任务开始执行的时间
3. private final long period; 周期执行任务的时间周期。大于0表示周期执行fixed-rate,等于0表示只执行一次,小于0表示周期延迟执行fixed-delay
4. RunnableScheduledFuture<V> outerTask = this; 当前ScheduledFutureTask对象的引用,为了reExecute重新执行时可以找到对象
5. int heapIndex; 延迟队列的索引号
ScheduledFutureTask的构造函数如下
第一个构造函数支持Runnable接口,表示只执行一次
第二个构造函数支持Callable接口,表示只执行一次
第三个构造函数传入period,period大于0表示fixed-rate,0表示执行一次,小于0表示fixed-delay
ns表示开始执行任务的时间,用纳秒表示
ScheduledFutureTask(Runnable r, V result, long ns) { super(r, result); this.time = ns; this.period = 0; this.sequenceNumber = sequencer.getAndIncrement(); } ScheduledFutureTask(Callable<V> callable, long ns) { super(callable); this.time = ns; this.period = 0; this.sequenceNumber = sequencer.getAndIncrement(); }ScheduledFutureTask(Runnable r, V result, long ns, long period) { super(r, result); this.time = ns; this.period = period; this.sequenceNumber = sequencer.getAndIncrement(); } 几个重要的方法,首先是和时间相关的方法
1. setNextRunTime()计算下次执行任务的时间。可以看到,当period大于0时表示fixed-rate的方式周期执行,每次执行的时间是固定的,从第一次开始执行,然后每次加上period,一旦开始运行就可以计算出每次执行的时间。
当period小于0时,表示fixed-delay方式周期执行,每次是取当前时间 + delay,当前时间是前一个任务执行完成的时间。受到当前任务执行时间的影响
2. comparedTo是Delayd接口要实现的方法,计算两个ScheduledFutureTask直接的时间间隔
private void setNextRunTime() { long p = period; if (p > 0) time += p; else time = triggerTime(-p); } long triggerTime(long delay) { return now() + ((delay < (Long.MAX_VALUE >> 1)) ? delay : overflowFree(delay)); } public int compareTo(Delayed other) { if (other == this) // compare zero ONLY if same object return 0; if (other instanceof ScheduledFutureTask) { ScheduledFutureTask<?> x = (ScheduledFutureTask<?>)other; long diff = time - x.time; if (diff < 0) return -1; else if (diff > 0) return 1; else if (sequenceNumber < x.sequenceNumber) return -1; else return 1; } long d = (getDelay(TimeUnit.NANOSECONDS) - other.getDelay(TimeUnit.NANOSECONDS)); return (d == 0) ? 0 : ((d < 0) ? -1 : 1); }
先判断是否是周期执行,如果Executor当前状态不能执行,就cancel,否则如果不是周期执行,就直接使用父类FutureTask的run方法即可。
如果是周期性执行任务,那么调用FutureTask的runAndReset方法,执行任务,然后重置ExecutorService状态。如果执行成功,就获取下次执行的时间,再调用reExecutePeriodic把任务加入工作队列
public void run() { boolean periodic = isPeriodic(); if (!canRunInCurrentRunState(periodic)) cancel(false); else if (!periodic) ScheduledFutureTask.super.run(); else if (ScheduledFutureTask.super.runAndReset()) { setNextRunTime(); reExecutePeriodic(outerTask); } } void reExecutePeriodic(RunnableScheduledFuture<?> task) { if (canRunInCurrentRunState(true)) { super.getQueue().add(task); if (!canRunInCurrentRunState(true) && remove(task)) task.cancel(false); else ensurePrestart(); } }
DelayedWorkQueue的目的是将工作队列组织成按照delay时间排序的队列,它存放的任务是RunnableScheduledFuture接口类型,实际实现的时候,对ScheduledFutureTask类型做了优化,ScheduledFutureTask维护了一个heapIndex,在取消任务时,需要将ScheduledFutureTask移出工作队列,heapIndex降低了查找的时间复杂度。
DelayedWorkQueue需要注意的是2点
1. 出入队列时,根据Delayed接口进行排序,delay越小越靠近队首,越早出队列
2. 采用了Leader/Follower模式优化了多线程取任务的模型,减少了多线程的竞争
siftUp和siftDown方法会根据ScheduledFutureTask的dealy时间调整它在工作队列中的位置,注意提升和下降时,没有和相邻的节点比较,而是按照2的倍数的位置来比较
private void siftUp(int k, RunnableScheduledFuture key) { while (k > 0) { int parent = (k - 1) >>> 1; RunnableScheduledFuture e = queue[parent]; if (key.compareTo(e) >= 0) break; queue[k] = e; setIndex(e, k); k = parent; } queue[k] = key; setIndex(key, k); } private void siftDown(int k, RunnableScheduledFuture key) { int half = size >>> 1; while (k < half) { int child = (k << 1) + 1; RunnableScheduledFuture c = queue[child]; int right = child + 1; if (right < size && c.compareTo(queue[right]) > 0) c = queue[child = right]; if (key.compareTo(c) <= 0) break; queue[k] = c; setIndex(c, k); k = child; } queue[k] = key; setIndex(key, k); }
offer入队列的逻辑很清晰
1. 首先判断队列长度是否足够,不够就扩容。
2. 如果进入空队列,那么入队任务就是队首。
3. 如果入的不是空队列,就按照入队任务的dealy时间调整它在队列中的位置
4. 如果入队任务成为队首,那么就按照Leader/Follower模式,唤醒一个在available条件队列上等待的Follower线程去做新的leader。
public boolean offer(Runnable x) { if (x == null) throw new NullPointerException(); RunnableScheduledFuture e = (RunnableScheduledFuture)x; final ReentrantLock lock = this.lock; lock.lock(); try { int i = size; if (i >= queue.length) grow(); size = i + 1; if (i == 0) { queue[0] = e; setIndex(e, 0); } else { siftUp(i, e); } if (queue[0] == e) { leader = null; available.signal(); } } finally { lock.unlock(); } return true; }
1. 先要获取全局锁,take是串行操作。当队列空时,线程在available条件队列上阻塞等待
2. 如果队列不为空,先计算任务的delay时间,如果delay <=0了,表示任务可以被执行了,就finishPoll,返回队首任务
3. 如果delay >0 表示队首任务还要等待一段时间,如果leader !=null,表示已经有leader在等待了,其他线程都是follwer,在available条件队列等待
4. 如果leader == null,表示这个当前线程可以成为leader,把当前线程设置为leader,然后限时等待delay时间。等待结束后,如果当前线程是是leader,就把leader设置为空,可以让从follower中唤醒一个成为leader。当前线程再次循环,此时dealy<0,可以返回了
5. 注意最外层的finally是在return finishPoll之前执行的,看看如何从follower中唤醒一个来作leader的: if(leader==null && queue[0] != null){available.signal()}
总结一下Leader/Follower模式,该模式可以减少多线程在取任务时的竞争。leader总是只有一个,follower线程在条件队列等待。等Leader获取到任务后,它先唤醒一个follower成为新的leader,然后再去执行任务。
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(TimeUnit.NANOSECONDS); if (delay <= 0) return finishPoll(first); else 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(); } }
构造函数默认使用了DelayedWorkQueue作为工作队列
public ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler) { super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, new DelayedWorkQueue(), threadFactory, handler); }
public void execute(Runnable command) { schedule(command, 0, TimeUnit.NANOSECONDS); } public Future<?> submit(Runnable task) { return schedule(task, 0, TimeUnit.NANOSECONDS); } public <T> Future<T> submit(Runnable task, T result) { return schedule(Executors.callable(task, result), 0, TimeUnit.NANOSECONDS); } public <T> Future<T> submit(Callable<T> task) { return schedule(task, 0, TimeUnit.NANOSECONDS); }
创建ScheduleFutureTask时,使用了triggerTime计算出了这个任务应该被触发的时间
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) { if (command == null || unit == null) throw new NullPointerException(); RunnableScheduledFuture<?> t = decorateTask(command, new ScheduledFutureTask<Void>(command, null, triggerTime(delay, unit))); delayedExecute(t); return t; } long triggerTime(long delay) { return now() + ((delay < (Long.MAX_VALUE >> 1)) ? delay : overflowFree(delay)); }
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(); } }
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<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; } /** * @throws RejectedExecutionException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { if (command == null || unit == null) throw new NullPointerException(); if (delay <= 0) throw new IllegalArgumentException(); ScheduledFutureTask<Void> sft = new ScheduledFutureTask<Void>(command, null, triggerTime(initialDelay, unit), unit.toNanos(-delay)); RunnableScheduledFuture<Void> t = decorateTask(command, sft); sft.outerTask = t; delayedExecute(t); return t; } protected boolean runAndReset() { if (state != NEW || !UNSAFE.compareAndSwapObject(this, runnerOffset, null, Thread.currentThread())) return false; boolean ran = false; int s = state; try { Callable<V> c = callable; if (c != null && s == NEW) { try { c.call(); // don't set result ran = true; } catch (Throwable ex) { setException(ex); } } } finally { // runner must be non-null until state is settled to // prevent concurrent calls to run() runner = null; // state must be re-read after nulling runner to prevent // leaked interrupts s = state; if (s >= INTERRUPTING) handlePossibleCancellationInterrupt(s); } return ran && s == NEW; }
最后再来梳理一下和任务周期执行相关的点
1. 同一个任务的周期性执行是由period参数控制的,大于0按照fixed-rate周期执行,等于0就执行一次,小于0按照fixed-delay周期执行。fixed-rate和fixed-delay的差别是前者从第一次开始执行就确定了后续每次执行的时间。 time += period。后者每次在前一个任务执行完成后,取当前时间now+delay计算得出。
不管是fixed-rate还是fixed-delay,必须等前一个任务执行完成后,才会把这个任务再次加入工作队列。所以如果任务执行时间超过了period,那么每次开始执行任务的时间是和任务执行时间有关系的。
2. 对于同一个任务来说,如果前一个任务执行失败,后续任务将会取消
3. 如果有多个工作线程执行周期性任务,工作线程worker在从工作队列取任务getTask时,调用了工作队列的take方法取任务,这个方法是阻塞队列的操作。ScheduledFutureTask的take方法实现时,会判断队首任务的delay时间是否到期,如果没到期,worker会阻塞直到delay时间到期才能取到队首任务。
4. ScheduledThreadPoolExecutor可以执行多个周期任务,每个周期任务之间是隔离的。对同一个任务周期执行来说,满足1-3点。