JAVA 延时队列

/**
 * Created by yl on 2017/7/10.
 */
public class Message implements Delayed{
    private int id;
    private String msg;
    private long excuteTime;

    public Message(int id, String msg, long excuteTime) {
        this.id = id;
        this.msg = msg;
        this.excuteTime = excuteTime;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public long getExcuteTime() {
        return excuteTime;
    }

    public void setExcuteTime(long excuteTime) {
        this.excuteTime = excuteTime;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.excuteTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS));
    }

}

队列操作:

/**
 * Created by yl on 2017/7/10.
 */
public class MessageDelayQueue {

    private MessageDelayQueue(){

    }
    private static DelayQueue<Message> queue = new DelayQueue<Message>();

    /*新增延时队列*/
    public static void addMessage(Message msg){
        queue.offer(msg);
    }
    /*移除延时队列*/
    public static void rmMessage(Message msg){
        queue.remove(msg);
    }

    public static DelayQueue<Message> getQueue() {
        return queue;
    }

    public static void setQueue(DelayQueue<Message> queue) {
        MessageDelayQueue.queue = queue;
    }
}

线程执行类

/**
 * Created by yl on 2017/7/10.
 */
public class DelayQueueThread implements Runnable {

    @Override
    public void run() {
        while(true) {
            try {
                Message take = MessageDelayQueue.getQueue().take();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                System.out.println(String.format("消息执行,msg:[%s],id:[%s],执行时间:[%s]", take.getMsg(), take.getId(), sdf.format(take.getExcuteTime())));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

测试类

public class Test {
    public static void main(String[] args) {
        MessageDelayQueue.addMessage(new Message(1,"消息1",getTime(15000)));
        MessageDelayQueue.addMessage(new Message(2,"消息2",getTime(30000)));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(String.format("启动延时任务[%s]",sdf.format(System.currentTimeMillis())));
        new Thread(new DelayQueueThread()).start();
        MessageDelayQueue.addMessage(new Message(3,"消息3",getTime(70000)));
        MessageDelayQueue.addMessage(new Message(4,"消息4",getTime(45000)));
    }

    public static long getTime(int s){
        return new Date().getTime() + s;
    }
}

最终结果分别15、30、45、70秒后执行:

启动延时任务[2017-07-10 13:43:04]
消息执行,msg:[消息1],id:[1],执行时间:[2017-07-10 13:43:19]
消息执行,msg:[消息2],id:[2],执行时间:[2017-07-10 13:43:34]
消息执行,msg:[消息4],id:[4],执行时间:[2017-07-10 13:43:49]
消息执行,msg:[消息3],id:[3],执行时间:[2017-07-10 13:44:14]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值