DelayQueue实现延迟队列

public class Q {
    public static void main(String[] args) throws Exception {
        DelayQueue<Order> orders = new DelayQueue<>();
        Order order1 = new Order(1000, "1x");
        Order order2 = new Order(2000, "2x");
        Order order3 = new Order(3000, "3x");
        Order order4 = new Order(4000, "4x");
        orders.add(order1);
        orders.add(order2);
        orders.add(order3);
        orders.add(order4);
        for (; ; ) {
            //没有到期会阻塞
            Order take = orders.take();
            System.out.println(take);
        }
    }
}

class Order implements Delayed {
    @Override
    public String toString() {
        return "DelayedElement{" + "delay=" + delayTime +
                ", expire=" + expire +
                ", data='" + data + '\'' +
                '}';
    }

    Order(long delay, String data) {
        delayTime = delay;
        this.data = data;
        expire = System.currentTimeMillis() + delay;
    }

    private final long delayTime; //延迟时间
    private final long expire;  //到期时间
    private String data;   //数据

    /**
     * 剩余时间=到期时间-当前时间
     */
    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.expire - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    /**
     * 优先队列里面优先级规则
     */
    @Override
    public int compareTo(Delayed o) {
        return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS));
    }
}
posted @ 2019-01-30 17:40  这谁顶着住啊  阅读(616)  评论(0编辑  收藏  举报