DelayedWorkQueue原理

使用最小堆实现,最近要到达时间的节点放在堆顶,每个节点都会附带到期时间,依次作为堆调整的依据。看poll()的源码就明白延迟队列的秘密了

        public RunnableScheduledFuture<?> poll() {
            final ReentrantLock lock = this.lock;
            lock.lock();
            try {
                RunnableScheduledFuture<?> first = queue[0];
                if (first == null || first.getDelay(NANOSECONDS) > 0)
                    return null;
                else
                    return finishPoll(first);
            } finally {
                lock.unlock();
            }
        }
上一篇:node.js事件循环


下一篇:java - 阻塞队列