1.订单实体
package com.zy.entity; import java.util.Date; import java.util.concurrent.Delayed; import java.util.concurrent.TimeUnit; public class Order implements Delayed { String id; String name;// 订单名称 Date creatTime;// 创建时间 int seconds; // 有效期n秒 Date endTime;// 过期时间 public Order(String id, String name, Date creatTime, int seconds) { super(); this.id = id; this.name = name; this.creatTime = creatTime; this.seconds = seconds; this.endTime = new Date(creatTime.getTime() + seconds * 1000);// 过期时间 } // 排序(插入时根据这个方法排序) @Override public int compareTo(Delayed o) { Order order = (Order) o; return this.seconds - order.seconds > 0 ? 1 : (this.seconds - order.seconds < 0 ? -1 : 0); } // 激活日期的剩余时间,时间单位由单位参数指定 @Override public long getDelay(TimeUnit unit) { long l = this.getEndTime().getTime() - System.currentTimeMillis(); return unit.convert(l,TimeUnit.MILLISECONDS); } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreatTime() { return creatTime; } public void setCreatTime(Date creatTime) { this.creatTime = creatTime; } public int getSeconds() { return seconds; } public void setSeconds(int seconds) { this.seconds = seconds; } public Date getEndTime() { return endTime; } public void setEndTime(Date endTime) { this.endTime = endTime; } }
2.下订单
package com.zy.service; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Stack; import java.util.concurrent.DelayQueue; import com.zy.entity.Order; public class OrderService implements Runnable { // delay队列(先进先出) DelayQueue<Order> queue = new DelayQueue<Order>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 添加订单(向队列中添加数据) public void insertOder(Order order) { System.out.println("向队列中添加订单(" + order.getName() + ")。。。。添加时间(" + sdf.format(order.getCreatTime()) + ")。。。。。" + "过期时间:" + sdf.format(order.getEndTime())); queue.put(order); } // 订单过期时间 @Override public void run() { while (true) { System.out.println("检查"); try { Order order = queue.take();// 当队列取出为空时,线程堵塞 if (order != null) { System.out.println(order.getName() + "订单支付时间过期" + sdf.format(new Date())); } else { System.out.println("无过期订单"); } } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) throws InterruptedException { // 启动线程 OrderService orderService = new OrderService(); new Thread(orderService).start(); // 项队列中添加数据 Order order1 = new Order("1", "订单一", new Date(), 50); Order order2 = new Order("2", "订单2", new Date(), 30); Order order3 = new Order("1", "订单3", new Date(), 40); orderService.insertOder(order1); orderService.insertOder(order2); orderService.insertOder(order3); Thread.currentThread().sleep(35000); Order order4 = new Order("4", "订单4", new Date(), 10); orderService.insertOder(order4); } }
3:结果
作者:逆舟
https://www.cnblogs.com/zy-jiayou/
本博客文章均为作者原创,转载请注明作者和原文链接。
https://www.cnblogs.com/zy-jiayou/
本博客文章均为作者原创,转载请注明作者和原文链接。