JDK之延时任务

2021/4/10 18:41:55

本文主要是介绍JDK之延时任务,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

JDK中延时任务可以使用

ScheduledThreadPoolExecutor.schedule 方法,其实这个方法和普通的线程池ThreadPoolExecutorService没啥区别,唯一的区别就是BlockingQueue
ScheduledThreadPoolExecutor使用的是它的内部类 
static class DelayedWorkQueue extends AbstractQueue<Runnable>
        implements BlockingQueue<Runnable> {

  

  奥秘其实就在这个DelayQueue的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 {
                        long delay = first.getDelay(NANOSECONDS);
                        if (delay <= 0)
                            return finishPoll(first);
                        first = null; // don't retain ref while waiting
                        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();
            }
        }

其中 

private final Condition available = lock.newCondition(); 
之前的文章分析过,DelayQueue里的元素不是普通的元素,而是必须实现了Delay接口的Class,接口方法是getDelay
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);
        }

 



这篇关于JDK之延时任务的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程