DelayQueue延时队列

本文详细介绍了Java的DelayQueue,它是一个实现了BlockingQueue接口的延迟队列,元素需要实现Delayed接口以设定延时时间。在获取元素时,只有当元素的延时时间已过才能被取出。文章通过代码示例展示了非阻塞和阻塞方式获取元素的方法,并给出了测试用例,展示了元素按延时时间升序或降序排列对队列操作的影响。

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

        DelayQueue是一个实现了BlockingQueue接口的类,它也是阻塞队列,但是他还具有一些独特的功能,那就是在获取元素时,需要检测元素是否到达延时时间,如果没有到达延时时间是无法获取到元素。

        队列里的元素需要实现Delay接口,这个接口有一个方法getDelay,当这个方法返回0或者负数,说明元素已经到达了延时时间,在获取元素时可以正常获取,否则无法获取。Delay接口继承了Comparable接口,重写它的compareTo方法,可以对队列中的元素排序,通常是按照延时时间从小到达排序,用于避免前面的元素未到达延时时间不能取出导致无法获取后面的元素。

        java延迟队列提供了在指定时间才能获取队列元素的功能,队列头元素是最接近过期的元素。没有过期元素的话,使用poll()方法会返回null值,超时判定是通过getDelay(TimeUnit.NANOSECONDS)方法的返回值小于等于0来判断。延时队列不能存放空元素。

   

public interface Delayed extends Comparable<Delayed> {

    /**
     * Returns the remaining delay associated with this object, in the
     * given time unit.
     *
     * @param unit the time unit
     * @return the remaining delay; zero or negative values indicate
     * that the delay has already elapsed
     */
    long getDelay(TimeUnit unit);
}

 由Delayed定义可以得知,队列元素需要实现getDelay(TimeUnit unit)方法和compareTo(Delayed o)方法, getDelay定义了剩余到期时间,compareTo方法定义了元素排序规则,注意,元素的排序规则影响了元素的获取顺。

获取元素:非阻塞

 public E poll() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            E first = q.peek();
            if (first == null || first.getDelay(NANOSECONDS) > 0)
                return null;
            else
                return q.poll();
        } finally {
            lock.unlock();
        }
    }

 获取元素:阻塞


    public E take() throws InterruptedException {
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            for (;;) {
                E first = q.peek();
                if (first == null)
                    available.await();
                else {
                    long delay = first.getDelay(NANOSECONDS);
                    if (delay <= 0)
                        return q.poll();
                    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 && q.peek() != null)
                available.signal();
            lock.unlock();
        }
    }

测试:

package com.tech.netty.test;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author lw
 * @since 2021/9/6
 */
public class Item implements Delayed {

    private int num;
    private long time;

    public Item(int num, long delay) {
        this.num = num;
        this.time = delay+System.currentTimeMillis();
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return time-System.currentTimeMillis();
    }
    
    //设置按照延时时间从小到大排列
    @Override
    public int compareTo(Delayed o) {
        Item o1 = (Item) o;
        long diff=time-o1.time;
        if(diff<=0){
            return -1;
        }else{
            return 1;
        }
    }

    @Override
    public String toString() {
        return "Item{" +
                "num=" + num +
                ", time=" + time +
                '}';
    }
}
package com.tech.netty.test;

import java.util.concurrent.DelayQueue;

/**
 * @author lw
 * @since 2021/9/6
 */
public class DQueue {
    public static void main(String[] args) throws InterruptedException {
        DelayQueue<Item> delayQueue=new DelayQueue<Item>();
        Item item1 = new Item(1, 3000);
        Item item2 = new Item(2, 6000);
        Item item3= new Item(3, 9000);
        delayQueue.put(item2);
        delayQueue.put(item1);
        delayQueue.put(item3);
        int size = delayQueue.size();
        for (int i = 0; i <size ; i++) {
            System.out.println(delayQueue.take());
        }
    }
}
可以发现元素是按延时时间升序,每间隔3秒输出一个

 如果设置元素按到期时间从大到小排列,则会发现元素是按延时时间倒序排列,几乎同时输出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值