JAVA 延时队列

来源:互联网 发布:算命软件哪个最准最全 编辑:程序博客网 时间:2024/06/06 03:05
/** * 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]
原创粉丝点击