延时队列

最近刚学,本篇先给出一个实际使用的例子。
首先队列对象当然就是DelayQueue。而队列元素则需要实现Delayed这个接口,并实现该接口compareTo方法和getDelay方法。

首先定义该元素及其属性。

class DelayTask implements Delayed {
    public String name;
    public Long delayTime;
    public TimeUnit delayTimeUnit;
    public Long executeTime;//ms

    DelayTask(String name, long delayTime, TimeUnit delayTimeUnit) {
        this.name = name;
        this.delayTime = delayTime;
        this.delayTimeUnit = delayTimeUnit;
        this.executeTime = System.currentTimeMillis() + delayTimeUnit.toMillis(delayTime);
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

getDelay方法的作用即是计算当前时间到执行时间之间还有多长时间。
如下,返回unit单位下该延迟时间的值。

@Override
public long getDelay(TimeUnit unit) {
    return unit.convert(executeTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}
 
 
  • 1
  • 2
  • 3
  • 4

compareTo方法的作用即是判断队列中元素的顺序谁前谁后。当前元素比队列元素后执行时,返回一个正数,比它先执行时返回一个负数,否则返回0.

@Override
public int compareTo(Delayed o) {
    if(this.getDelay(TimeUnit.MILLISECONDS) > o.getDelay(TimeUnit.MILLISECONDS)) {
        return 1;
    }else if(this.getDelay(TimeUnit.MILLISECONDS) < o.getDelay(TimeUnit.MILLISECONDS)) {
        return -1;
    }
    return 0;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

最后我们用个简单的方法测试下:

public static void main(String[] args) {
    DelayQueue<DelayTask> queue = new DelayQueue<>();
    queue.add(new DelayTask("1", 1L, TimeUnit.SECONDS));
    queue.add(new DelayTask("2", 2L, TimeUnit.SECONDS));
    queue.add(new DelayTask("3", 3L, TimeUnit.SECONDS));

    System.out.println("queue put done");

    while(!queue.isEmpty()) {
        try {
            DelayTask task = queue.take();
            System.out.println(task.name + ":" + System.currentTimeMillis());

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

运行结果如下:

queue put done
1:1504498317145
2:1504498318145
3:1504498319145

 
 
  • 1
  • 2
  • 3
  • 4
  • 5

下面是完整的代码:

public class DelayQueueTest {
    public static void main(String[] args) {
        DelayQueue<DelayTask> queue = new DelayQueue<>();
        queue.add(new DelayTask("1", 1000L, TimeUnit.MILLISECONDS));
        queue.add(new DelayTask("2", 2000L, TimeUnit.MILLISECONDS));
        queue.add(new DelayTask("3", 3000L, TimeUnit.MILLISECONDS));

    System.<span class="hljs-keyword">out</span>.println(<span class="hljs-string">"queue put done"</span>);

    <span class="hljs-keyword">while</span>(!queue.isEmpty()) {
        <span class="hljs-keyword">try</span> {
            DelayTask task = queue.take();
            System.<span class="hljs-keyword">out</span>.println(task.name + <span class="hljs-string">":"</span> + System.currentTimeMillis());

        } <span class="hljs-keyword">catch</span> (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

}
class DelayTask implements Delayed {
public String name;
public Long delayTime;
public TimeUnit delayTimeUnit;
public Long executeTime;//ms

DelayTask(String name, <span class="hljs-keyword">long</span> delayTime, TimeUnit delayTimeUnit) {
    <span class="hljs-keyword">this</span>.name = name;
    <span class="hljs-keyword">this</span>.delayTime = delayTime;
    <span class="hljs-keyword">this</span>.delayTimeUnit = delayTimeUnit;
    <span class="hljs-keyword">this</span>.executeTime = System.currentTimeMillis() + delayTimeUnit.toMillis(delayTime);
}


@Override
<span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">compareTo</span>(Delayed o) {
    <span class="hljs-keyword">if</span>(<span class="hljs-keyword">this</span>.getDelay(TimeUnit.MILLISECONDS) &gt; o.getDelay(TimeUnit.MILLISECONDS)) {
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
    }<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(<span class="hljs-keyword">this</span>.getDelay(TimeUnit.MILLISECONDS) &lt; o.getDelay(TimeUnit.MILLISECONDS)) {
        <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;
    }
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}

@Override
<span class="hljs-keyword">public</span> <span class="hljs-keyword">long</span> <span class="hljs-title">getDelay</span>(TimeUnit unit) {
    <span class="hljs-keyword">return</span> unit.convert(executeTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值