JDK對ScheduledThreadPoolExecutor 的描述
一個ThreadPoolExecutor可以添加調度命令在一個指定的延時時間或週期性的時間執行。相比Timer類它支持多線程。延時任務到了能執行的時間馬上執行但是沒有任何實時的保證。到了設定的時候候,他們將會開始。任務調用正好完全相同的時間按提交的先進先出的順序執行。
和ThreadPoolExecutor相比需要注意的
這個類繼承自ThreadPoolExecutor,繼承的方法有一點地方不能在這個類使用。 特別的,因爲它通過一個固定的線程池使用corePoolSize數量線程別切一個無界隊列調整maximumPoolSize沒有有效的作用。另外的,這通常不是一個好主意去設置corePoolSize爲0或允許corePool線程超時,因爲它可能讓線程池中沒有線程去處理任務一旦他們變得可以執行。
取消方面的說明
當一個提交任務在它運行錢被取消,執行會被抑制。默認,一個被取消的任務不會 * 自動的從工作隊列中移除直到它的延時時間到。這個可以更進一步的檢測和監視, * 也會導致取消任務的無限制保留。爲了避免這個發生,設置setRemoveOnCancelPolicy 爲true會讓取消的任務馬上從工作隊列中移除。
關於週期性任務的執行
一個任務被連續的執行通過scheduleAtFixedRate或scheduleWithFixedDelay方法不能重疊。也就是一次執行未完成但是到了下一個要執行的點了,下一個不能執行。不同執行可能被不同的線程執行,先被執行的發生在後面執行的之前。
當period大於0時,任務爲固定比率的執行。任務的下次執行時間都是在原來執行時間(time)的基礎上加上間隔時間(period)。如果任務執行時間比較長,在任務還未執行完時下次再次執行任務的時間到來不會同時去執行兩個相同的任務,需要等本次執行完後再馬上去執行下次的任務。如下圖:
當period小於0時,任務爲固定延時的執行。任務的下次執行時間在結束任務時當前系統時間的基礎上加上間隔時間(period)。
如下圖:
關於任務類
任務類繼承了FurureTask,實現了RunnableScheduledFuture接口。比較需要的注意的地方是它添加了heapIndex字段去記錄任務在隊列中的下標,因爲任務隊列是堆數據結構,這樣可以簡化從任務隊列中移除和取消任務的操作。在任務被執行或取消後這個下標值都是被設置爲-1。getDelay方法也是比較需要注意的在後面很多地方有使用到。compareTo也是一定要的,後面的優先級隊列會使用這個方法進行任務的排序建堆。
private class ScheduledFutureTask<V>
extends FutureTask<V> implements RunnableScheduledFuture<V> {
/** Sequence number to break ties FIFO */
/**序列號去打破先進先出*/
private final long sequenceNumber;
/** The time the task is enabled to execute in nanoTime units */
/**任務能被啓動執行的時間,單位:納秒*/
private long time;
/**
* Period in nanoseconds for repeating tasks. A positive
* value indicates fixed-rate execution. A negative value
* indicates fixed-delay execution. A value of 0 indicates a
* non-repeating task.
* 重複執行任務的間隔時間單位納秒,一個正數表示固定比率的執行。
* 一個負數表示固定延時的執行。如果爲0表示不用重複執行
*/
private final long period;
/** The actual task to be re-enqueued by reExecutePeriodic */
RunnableScheduledFuture<V> outerTask = this;
/**
* Index into delay queue, to support faster cancellation.
* 任務在延時隊列中的索引,爲了更快的取消
*/
int heapIndex;
/**
* Creates a one-shot action with given nanoTime-based trigger time.
*/
ScheduledFutureTask(Runnable r, V result, long ns) {
super(r, result);
this.time = ns;
this.period = 0;
this.sequenceNumber = sequencer.getAndIncrement();
}
/**
* Creates a periodic action with given nano time and period.
*/
ScheduledFutureTask(Runnable r, V result, long ns, long period) {
super(r, result);
this.time = ns;
this.period = period;
this.sequenceNumber = sequencer.getAndIncrement();
}
/**
* Creates a one-shot action with given nanoTime-based trigger time.
*/
ScheduledFutureTask(Callable<V> callable, long ns) {
super(callable);
this.time = ns;
this.period = 0;
this.sequenceNumber = sequencer.getAndIncrement();
}
/**獲取還需多久開始執行*/
public long getDelay(TimeUnit unit) {
return unit.convert(time - now(), NANOSECONDS);
}
/**重寫compareTo方法*/
public int compareTo(Delayed other) {
if (other == this) // compare zero if same object
return 0;
if (other instanceof ScheduledFutureTask) {
ScheduledFutureTask<?> x = (ScheduledFutureTask<?>)other;
long diff = time - x.time;
//如果當前任務執行時間小於參數的執行時間則返回-1,表示當前任務小於參數任務
if (diff < 0)
return -1;
//如果當前任務執行時間大於參數的執行時間則返回1,表示當前過任務大於參數任務
else if (diff > 0)
return 1;
//如果當前任務和參數任務的執行時間相等則比較sequenceNumber
else if (sequenceNumber < x.sequenceNumber)
return -1;
else
return 1;
}
long diff = getDelay(NANOSECONDS) - other.getDelay(NANOSECONDS);
return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
}
/**
* 返回true如果是一個週期性任務
* Returns {@code true} if this is a periodic (not a one-shot) action.
*
* @return {@code true} if periodic
*/
public boolean isPeriodic() {
return period != 0;
}
/**
* 設置下一次執行時間
* Sets the next time to run for a periodic task.
*/
private void setNextRunTime() {
long p = period;
//如果週期時間大於0
if (p > 0)
//在原來時間基礎上直接加上週期時間
time += p;
else
//如果週期時間小於0,則在當前時間的基礎上加上period
time = triggerTime(-p);
}
public boolean cancel(boolean mayInterruptIfRunning) {
boolean cancelled = super.cancel(mayInterruptIfRunning);
if (cancelled && removeOnCancel && heapIndex >= 0)
remove(this);
return cancelled;
}
/**
* Overrides FutureTask version so as to reset/requeue if periodic.
* 重寫了FutureTask的run方法
*/
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);
}
}
}
關於任務隊列
ScheduledThreadPoolExecutor 的任務隊列使用自己內部類DelayedWorkQueue延時工作隊列。
//初始容量16
private static final int INITIAL_CAPACITY = 16;
使用一個數組進行任務存儲。這個任務存儲隊列是一個基於二叉堆數據結構的優先級隊列。 使用二叉堆使得任務的移除和添加的時間複雜度都是O(logn)。這個隊列裏還是用了Leader-Follower模式的變種。
二叉堆上下移動的操作
/**
* Sifts element added at bottom up to its heap-ordered spot.
* Call only when holding lock.
* 完成節點上移。只有當持有鎖的時候才能進行操作
*
*/
private void siftUp(int k, RunnableScheduledFuture<?> key) {
while (k > 0) {
//找到父節點
int parent = (k - 1) >>> 1;
RunnableScheduledFuture<?> e = queue[parent];
if (key.compareTo(e) >= 0)
//如果大於父節點則跳出,將key設置到queue[k]
break;
//將父節點賦值到k的地方
queue[k] = e;
//設置任務的下標
setIndex(e, k);
//將k的值修改爲父節點下標
k = parent;
}
queue[k] = key;
//設置任務下標
setIndex(key, k);
}
/**
* Sifts element added at top down to its heap-ordered spot.
* Call only when holding lock.
* 節點下移
* 只有當持有鎖的時候才能進行操作
*/
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];
//將較小的孩子和key進行比較,如果k小於孩子則跳出循環
if (key.compareTo(c) <= 0)
break;
//將較小的孩子節點上移
queue[k] = c;
//設置任務的下標
setIndex(c, k);
k = child;
}
queue[k] = key;
setIndex(key, k);
}
隊列擴容
/**
* Resizes the heap array. Call only when holding lock.
* 只有在持有鎖的情況下才能進行擴容。每次擴容百分之五十。
*/
private void grow() {
int oldCapacity = queue.length;
int newCapacity = oldCapacity + (oldCapacity >> 1); // grow 50%
if (newCapacity < 0) // overflow
newCapacity = Integer.MAX_VALUE;
queue = Arrays.copyOf(queue, newCapacity);
}
移除
/**
* 移除指定元素
* @param x
* @return
*/
public boolean remove(Object x) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
//找出下標
int i = indexOf(x);
if (i < 0)
return false;
//將任務下標置爲-1
setIndex(queue[i], -1);
//隊列大小減一
int s = --size;
RunnableScheduledFuture<?> replacement = queue[s];
queue[s] = null;
//如果要移除的元素正好是最後一位就不必進行堆移位操作。
if (s != i) {
//先進行下移
siftDown(i, replacement);
//如果i的位置正好是replacement,則在進行上移。
if (queue[i] == replacement)
siftUp(i, replacement);
}
return true;
} finally {
lock.unlock();
}
}
/**
* 插入元素
* @param x
* @return
*/
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);
}
//如果當前任務被設置爲隊列首位則,將leader置爲null,喚醒其他線程
if (queue[0] == e) {
leader = null;
available.signal();
}
} finally {
lock.unlock();
}
return true;
}
獲取任務的幾個方法
/**
* Performs common bookkeeping for poll and take: Replaces
* first element with last and sifts it down. Call only when
* holding lock.
* @param f the task to remove and return
*/
private RunnableScheduledFuture<?> finishPoll(RunnableScheduledFuture<?> f) {
//任務到了執行時間
//隊列大小減一
int s = --size;
//將隊尾的任務放到隊首,開始下移
RunnableScheduledFuture<?> x = queue[s];
queue[s] = null;
if (s != 0)
siftDown(0, x);
//將要出隊的任務設置爲負一
setIndex(f, -1);
return f;
}
public RunnableScheduledFuture<?> poll() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
RunnableScheduledFuture<?> first = queue[0];
//如果隊列首位爲null或首位的任務還沒有到達執行時間則返回null
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)
//如果小於等於0說明可以開始執行,則調用finishPoll方法
//進行堆重排序,隊列大小減一等操作。並返回任務
return finishPoll(first);
first = null; // don't retain ref while waiting
//如果leader不爲空則阻塞當前線程。
//這裏的leader如果不爲空,說明已經有線程在等待隊首的任務指定時間流逝。
if (leader != null)
available.await();
else {
//如果leader爲空則將當前線程設置爲leader.
Thread thisThread = Thread.currentThread();
leader = thisThread;
try {
//阻塞隊首任務的時間
available.awaitNanos(delay);
} finally {
//如果leader爲當前線程則將leader設置爲空。
if (leader == thisThread)
leader = null;
}
}
}
}
} finally {
//如果leader爲空,且隊列爲空則阻塞當前線程
if (leader == null && queue[0] != null)
available.signal();
lock.unlock();
}
}
public RunnableScheduledFuture<?> poll(long timeout, TimeUnit unit)
throws InterruptedException {
long nanos = unit.toNanos(timeout);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
for (;;) {
RunnableScheduledFuture<?> first = queue[0];
//如果隊列爲空
if (first == null) {
//如果指定時間流逝完,則返回null
if (nanos <= 0)
return null;
else
//阻塞指定時間
nanos = available.awaitNanos(nanos);
} else {
long delay = first.getDelay(NANOSECONDS);
//如果到了隊列首位任務的執行時間則返回
if (delay <= 0)
return finishPoll(first);
//如果指定時間流逝完則返回null
if (nanos <= 0)
return null;
first = null; // don't retain ref while waiting
//如果任務還要等待的時間大於指定的時間或leader不爲空,
//leader不爲null說明有線程在等待隊列首位的任務
if (nanos < delay || leader != null)
nanos = available.awaitNanos(nanos);
else {
//將leader設置爲當前線程
Thread thisThread = Thread.currentThread();
leader = thisThread;
try {
//等待隊首任務的還需要等待的時間
long timeLeft = available.awaitNanos(delay);
nanos -= delay - timeLeft;
} finally {
//如果leader還爲當前線程則置爲null
if (leader == thisThread)
leader = null;
}
}
}
}
} finally {
if (leader == null && queue[0] != null)
available.signal();
lock.unlock();
}
}
構造方法
相對比ThreadPoolExecotor的構造方法,SchedulePoolExecutore的構造方法的參數就少很多。因爲它的核心工作線程和最大工作線程是一樣的,他的線程都會等待任務執行時間的到來,所以不需要線程的存活時間和最大線程參數。任務隊列是自己內部類不需要傳遞參數。所以最多隻用設置核心線程數和線程工廠類。
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);
}
任務提交
任務提交除了下面的幾個方法還有重寫ThreadPoolExecutor的submit等方法,但是他們的延時時間都是0,都會立即執行。
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;
}
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;
}
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;
}
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));//固定延時delay設置爲負數
RunnableScheduledFuture<Void> t = decorateTask(command, sft);
sft.outerTask = t;
delayedExecute(t);
return t;
}
這裏面都會去調用一個delayedExecute方法。在這個方法中會判斷線程池是否有被shutdown,如果關閉了會直接拒絕任務。這個方法中在開始判斷shutdown後任務入隊後又再次判斷了一次線程池是否關閉。
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
//如果線程數少於核心工作線程數則創建,如果核心線程數設置爲0則至少會設置一個工作線程
ensurePrestart();
}
}
void ensurePrestart() {
int wc = workerCountOf(ctl.get());
if (wc < corePoolSize)
addWorker(null, true);
else if (wc == 0)
addWorker(null, false);
}
執行時間溢出的處理
ScheduledFutureTask中執行時間time使用的是long類型,但是如果有設置時間間隔比較大,相加後就會溢出。ScheduleThreadPoolExecutor的任務存放隊列是優先級隊列,依靠的是ScheduledFutureTask的compareTo方法。而這個方法中主要是比較任務的執行時間。如果時間溢出由正數變爲負數則會影響這個任務在任務隊列中的位置。所以JDK對這種情況做了處理。
long triggerTime(long delay) {
return now() +
((delay < (Long.MAX_VALUE >> 1)) ? delay : overflowFree(delay));
}
private long overflowFree(long delay) {
Delayed head = (Delayed) super.getQueue().peek();
if (head != null) {
long headDelay = head.getDelay(NANOSECONDS);
if (headDelay < 0 && (delay - headDelay < 0))
delay = Long.MAX_VALUE + headDelay;
}
return delay;
}
具體的可以看看這篇博客,裏面有實際例子分析。其實就是數字的溢出再溢出。