品易云推流 关闭
文章详情页
文章 > java基础 > java中DelayQueue入队方法

java中DelayQueue入队方法

头像

小妮浅浅

2021-02-09 12:49:292056浏览 · 0收藏 · 0评论

本教程操作环境:windows7系统、java10版,DELL G3电脑。

1.入队说明

因为DelayQueue是阻塞队列,且优先级队列是无界的,所以入队不会阻塞不会超时,因此它的四个入队方法是一样的。

2.入队过程

(1)加锁;

(2)添加元素到优先级队列中;

(3)如果添加的元素是堆顶元素,就把leader置为空,并唤醒等待在条件available上的线程;

(4)解锁

3.实例

1add

将指定的元素插入到此队列中,在成功时返回 true

public boolean add(E e) {
        return offer(e);
}

2offer

将指定的元素插入到此队列中,在成功时返回 true,在前面的add 中,内部调用了offer 方法,我们也可以直接调用offer 方法来完成入队操作。

    /**
     * Inserts the specified element into this delay queue. As the queue is
     * unbounded this method will never block.
     *
     * @param e the element to add
     * @param timeout This parameter is ignored as the method never blocks
     * @param unit This parameter is ignored as the method never blocks
     * @return {@code true}
     * @throws NullPointerException {@inheritDoc}
     */
    public boolean offer(E e, long timeout, TimeUnit unit) {
        //调用offer 方法
        return offer(e);
}

以上就是java中DelayQueue入队方法,在对其入队基本知识了解后,就可以在代码部分进行实战练习了,学会了赶快行动起来吧。

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图