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]
阅读全文
0 0
- java延时队列使用
- JAVA 延时队列
- linux 延时工作队列
- 延时工作队列INIT_DELAYED_WORK
- live555延时队列
- RabbitMQ 实现延时队列
- laravel 5.2队列延时
- 单机版延时队列
- rabbitmq--延时队列
- JAVA延时
- JAVA延时
- JAVA延时
- JAVA延时
- 简析LIVE555中的延时队列
- 简析LIVE555中的延时队列 .
- DelayQueue延时队列的使用
- RabbitMQ学习之延时队列
- 用mongodb构建延时队列
- 【Linux4.1.12源码分析】邻居子系统实现分析
- 测试题
- 构建者模式和抽象工厂模式的结合使用
- 带环形进度条的button
- Android 资源与属性备忘单
- JAVA 延时队列
- Ubuntu Server16安装Jenkins
- 简单的matplotlib用法
- java远程控制tomcat启动关机
- hibernate中opensession和getCurrentSession的区别
- string.ToString(System.IFormatProvider)”最匹配的重载方法具有一些无效参数
- 调研套接字server bind 失败的原因?——Address already in use
- SVM资料
- 【简记】Java Web 内幕——Spring源码(组件分析,BeanFactory源码,Bean创建之前)