阻塞queue系列之DelayQueue

DelayQueue是一个无界阻塞队列,只有在延迟期满时才能从中提取元素。该队列的头部是延迟期满后保存时间最长的Delayed 元素。底层也是通过数组实现,所以读写操作使用的是同一把锁,其插入的元素必须实现Delayed接口。

构造方法

public DelayQueue() {}

public DelayQueue(Collection<? extends E> c) {
        this.addAll(c);
    }

Delayed接口

public interface Delayed extends Comparable<Delayed> {

    long getDelay(TimeUnit unit);
}

Delayed接口继承了Comparable接口,所以当我们插入的元素实现Delayed接口时,须实现两个方法getDelay(TimeUnit unit)和compareTo(Delayed o),这两个方法将在下面源码中介绍。

常用方法

  • offer 添加元素
    这里写图片描述
    这里写图片描述

其中q是PriorityQueue q = new PriorityQueue(); 可见其实现是通过PriorityQueue实现。

add和put都是调用offer方法,他们三者无任何区别,添加元素都是立刻添加的,无延迟。

siftUp(i, e) 里面是什么呢?
这里写图片描述
看到这里是不是感觉熟悉,是的这里类似PriorityBlockingQueue,因为comparator为null, 会使用插入元素实现了Comparatable接口的方法进行比较,按照顺序插入。(解释了compareTo(Delayed o)方法)


  • peek 取第一个元素,不会删除

这里写图片描述
可见,和其它阻塞队列的peek方法一样,也是立刻取值,并不延迟。


  • poll 取元素
    这里写图片描述

first.getDelay(NANOSECONDS) > 0 可以看出,当取的元素是null或者未到期时,这时就会返回null.
getDelay方法就是接口Delayed接口的方法,也就是插入元素的方法(插入元素实现Delayed接口)。(解释了getDelay(TimeUnit unit)方法)。


  • take 取元素

这里写图片描述

take方法会阻塞,直到待取元素已经到期了。

案例

public class DelayQueueExample {
    private DelayQueue<DelayElement> queue;

    @Before
    public void Before() {
        queue = new DelayQueue();
    }

    @Test
    public void test() throws InterruptedException {
        queue.add(DelayElement.of("hello",TimeUnit.SECONDS,1));
        System.out.println(queue.size());
        //peek无延迟
        System.out.println(queue.peek());
        //若时间未到,返回null,若时间已到,取值
        System.out.println(queue.poll());
        //take延迟,阻塞至取到值
        System.out.println(queue.take().getE());
    }



    static class DelayElement<E> implements Delayed{
        private final E e;
        private final long dealy;

        DelayElement(E e, TimeUnit unit,long dealy) {
            this.e = e;
            this.dealy = TimeUnit.MILLISECONDS.convert(dealy,unit) + System.currentTimeMillis();
        }

        static <T>DelayElement<T> of(T e, TimeUnit unit,long dealy){
            return new DelayElement(e,unit, dealy);
        }

        static <T>DelayElement<T> of(T e,long dealy){
            return new DelayElement(e,TimeUnit.MILLISECONDS, dealy);
        }
        @Override
        public long getDelay(TimeUnit unit) {
            long l = dealy - System.currentTimeMillis();
            return unit.convert(l,TimeUnit.MICROSECONDS);
        }

        @Override
        public int compareTo(Delayed o) {
            DelayElement that = (DelayElement) o;
            if (this.dealy > that.getDealy()){
                return 1;
            } else if (this.dealy < that.getDealy()){
                return -1;
            } else {
                return 0;
            }
        }

        public E getE() {
            return e;
        }

        public long getDealy() {
            return dealy;
        }


        @Override
        public String toString() {
            return "DelayElement{" +
                    "e=" + e +
                    ", dealy=" + dealy +
                    '}';
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值