Executor(四):ScheduledThreadPoolExecutor jdk1.8

本文详细解析了JDK中ScheduledThreadPoolExecutor的实现原理,包括其任务提交、执行、取消及调度策略。探讨了任务类ScheduledFutureTask的设计,任务队列DelayedWorkQueue的运作机制,以及构造方法和执行时间溢出的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
    }

具体的可以看看这篇博客,里面有实际例子分析。其实就是数字的溢出再溢出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值