延迟队列 java_Java延迟队列

Java延迟队列

DelayQueue 实现 BlockingQueue 接口。 DelayQueue 中的元素必须保留一定的时间。

DelayQueue 使用一个名为 Delayed 的接口来获取延迟时间。

该接口在java.util.concurrent包中。 其声明如下:

public interface Delayed extends Comparable {

long getDelay(TimeUnit timeUnit);

}

它扩展了 Comparable 接口,它的 compareTo()方法接受一个Delayed对象。

DelayQueue 调用每个元素的 getDelay()方法来获取元素必须保留多长时间。 DelayQueue 将传递 TimeUnit 到此方法。

当 getDelay()方法返回一个零或一个负数时,是元素离开队列的时间。

队列通过调用元素的 compareTo()方法确定要弹出的那个。 此方法确定要从队列中删除的过期元素的优先级。

以下代码显示了如何使用DelayQueue。

import static java.time.temporal.ChronoUnit.MILLIS;

import static java.util.concurrent.TimeUnit.MILLISECONDS;

import java.time.Instant;

import java.util.concurrent.BlockingQueue;

import java.util.concurrent.DelayQueue;

import java.util.concurrent.Delayed;

import java.util.concurrent.TimeUnit;

class DelayedJob implements Delayed {

private Instant scheduledTime;

String jobName;

public DelayedJob(String jobName, Instant scheduledTime) {

this.scheduledTime = scheduledTime;

this.jobName = jobName;

}

@Override

public long getDelay(TimeUnit unit) {

long delay = MILLIS.between(Instant.now(), scheduledTime);

long returnValue = unit.convert(delay, MILLISECONDS);

return returnValue;

}

@Override

public int compareTo(Delayed job) {

long currentJobDelay = this.getDelay(MILLISECONDS);

long jobDelay = job.getDelay(MILLISECONDS);

int diff = 0;

if (currentJobDelay > jobDelay) {

diff = 1;

} else if (currentJobDelay < jobDelay) {

diff = -1;

}

return diff;

}

@Override

public String toString() {

String str = this.jobName + ", " + "Scheduled Time: "

+ this.scheduledTime;

return str;

}

}

public class Main {

public static void main(String[] args) throws InterruptedException {

BlockingQueue queue = new DelayQueue<>();

Instant now = Instant.now();

queue.put(new DelayedJob("A", now.plusSeconds(9)));

queue.put(new DelayedJob("B", now.plusSeconds(3)));

queue.put(new DelayedJob("C", now.plusSeconds(6)));

queue.put(new DelayedJob("D", now.plusSeconds(1)));

while (queue.size() > 0) {

System.out.println("started...");

DelayedJob job = queue.take();

System.out.println("Job: " + job);

}

System.out.println("Finished.");

}

}

上面的代码生成以下结果。

622a832a18840211df42fe6cebfca4dd.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值