补充部分---ScheduledThreadPoolExecutor类分析 线程池底层原理详解与源码分析( 三 )

3.而且获得负数在compareTo这一步不影响排序 。【可能是由于科技发展的缘故吧,现在Long.MAX_VALUE【9223372036854775807L】溢出了,就会变为-9223372036854775808L,对排序不影响】
【5】ScheduledThreadPoolExecutor类源码分析
1.ScheduledThreadPoolExecutor的四种使用方法
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {if (command == null || unit == null)throw new NullPointerException();RunnableScheduledFuture<Void> t = decorateTask(command, new ScheduledFutureTask<Void>(command, null, triggerTime(delay, unit), sequencer.getAndIncrement()));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), sequencer.getAndIncrement()));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 <= 0L)throw new IllegalArgumentException();//这里设置的-unit.toNanos(delay)是负数ScheduledFutureTask<Void> sft = new ScheduledFutureTask<Void>(command, null, triggerTime(initialDelay, unit), -unit.toNanos(delay), sequencer.getAndIncrement());//这个方法是用于以后做扩展的RunnableScheduledFuture<Void> t = decorateTask(command, sft);sft.outerTask = t;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 <= 0L)throw new IllegalArgumentException();//这里设置unit.toNanos(period)是正数ScheduledFutureTask<Void> sft = new ScheduledFutureTask<Void>(command, null,triggerTime(initialDelay, unit), unit.toNanos(period), sequencer.getAndIncrement());//这个方法是用于以后做扩展的RunnableScheduledFuture<Void> t = decorateTask(command, sft);sft.outerTask = t;delayedExecute(t);return t;}2.ScheduledThreadPoolExecutor类#triggerTime方法
//获取初始的延迟执行时间(以纳秒的形式,相当于我在哪个时间点要执行)private long triggerTime(long delay, TimeUnit unit) {return triggerTime(unit.toNanos((delay < 0) ? 0 : delay));}long triggerTime(long delay) {return System.nanoTime() + ((delay < (Long.MAX_VALUE >> 1)) ? delay : overflowFree(delay));}3.ScheduledThreadPoolExecutor类#delayedExecute方法
private void delayedExecute(RunnableScheduledFuture<?> task) {//如果处于非运行状态则拒绝任务(这个方法里面比较的是不是比关闭状态大)if (isShutdown())reject(task);else {//加入队列super.getQueue().add(task);//如果加入队列后canRunInCurrentRunState检测线程池,返回false则移除任务if (!canRunInCurrentRunState(task) && remove(task))task.cancel(false); //以不可中断方式执行完成执行中的调度任务elseensurePrestart();}}boolean canRunInCurrentRunState(RunnableScheduledFuture<?> task) {//如果处于运行状态返回trueif (!isShutdown())return true;//处于停止状态,整理状态,销毁状态,三者之一返回falseif (isStopped())return false;//处于关闭状态,返回run-after-shutdown参数return task.isPeriodic()? continueExistingPeriodicTasksAfterShutdown //默认false: (executeExistingDelayedTasksAfterShutdown|| task.getDelay(NANOSECONDS) <= 0);}void ensurePrestart() {int wc = workerCountOf(ctl.get());if (wc < corePoolSize) //保持工作者与核心线程数持平addWorker(null, true);else if (wc == 0) //即时核心线程是0,也至少会启动一个addWorker(null, false);}【6】DelayedWorkQueue类源码分析
0.DelayedWorkQueue类#核心属性
private static final int INITIAL_CAPACITY = 16;// 初始容量private RunnableScheduledFuture<?>[] queue = new RunnableScheduledFuture<?>[INITIAL_CAPACITY];// 控制并发和阻塞等待private final ReentrantLock lock = new ReentrantLock();private final Condition available = lock.newCondition(); //这个可以参考take方法与offer方法,个人觉得是采用中断方式唤醒持有锁的线程private int size; // 节点数量private Thread leader;//记录持有锁的线程(当等待的时候)

经验总结扩展阅读