阻塞队列DelayQueue的简单应用

DelayQueue

一个无界的阻塞队列,并且支持延时取出对象,前提对象要实现Delayed接口。此篇文章模拟下订单未付款,到期后关闭订单的一个过程。

 

package cn.bikan8;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @Author 小浩
 * @Date 2020/7/9 16:26
 * @Version 1.0
 **/
public class ItemVo<T> implements Delayed {
    private Long activeTime;
    private T object;

    /**
     * 构造方法
     * @param activeTime  默认单位是s
     * @param object
     */
    public ItemVo(Long activeTime, T object) {
        this.activeTime = TimeUnit.NANOSECONDS.convert(activeTime,TimeUnit.SECONDS)+System.nanoTime();
        this.object = object;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return this.activeTime-System.nanoTime();
    }

    @Override
    public int compareTo(Delayed o) {
        Long d = getDelay(TimeUnit.NANOSECONDS) -o.getDelay(TimeUnit.NANOSECONDS);
        return d==0?0:(d>0?1:-1);
    }

    public Long getActiveTime() {
        return activeTime;
    }

    public T getObject() {
        return object;
    }
}
package cn.bikan8;

import sun.dc.pr.PRError;

import java.io.Serializable;

/**
 * @Author 小浩
 * @Date 2020/7/9 17:07
 * @Version 1.0
 **/
public class Order implements Serializable {
    private String orderId;
    private String orderName;

    public Order() {
    }

    public Order(String orderId, String orderName) {
        this.orderId = orderId;
        this.orderName = orderName;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getOrderName() {
        return orderName;
    }

    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }

    @Override
    public String toString() {
        return "Order{" +
                "orderId='" + orderId + '\'' +
                ", orderName='" + orderName + '\'' +
                '}';
    }
}
package cn.bikan8;

import java.util.concurrent.DelayQueue;

/**
 * @Author 小浩
 * @Date 2020/7/9 17:09
 * @Version 1.0
 **/
public class PutOrder implements Runnable {
    private DelayQueue delayQueue;
    private Long delayTime;
    private  Order order;

    public PutOrder(DelayQueue delayQueue, Long delayTime,Order order) {
        this.delayQueue = delayQueue;
        this.delayTime = delayTime;
        this.order=order;
    }

    public PutOrder() {
    }

    @Override
    public void run() {
        delayQueue.put(new ItemVo<Order>(delayTime,order));
        System.out.println("生成订单"+order.getOrderId()+"剩余付款时间为"+delayTime+"s");

    }
}
package cn.bikan8;

import java.util.concurrent.DelayQueue;

/**
 * @Author 小浩
 * @Date 2020/7/9 17:15
 * @Version 1.0
 **/
public class RemoveOrder implements  Runnable {
    private DelayQueue delayQueue;

    public RemoveOrder(DelayQueue delayQueue) {
        this.delayQueue = delayQueue;
    }

    @Override
    public void run() {
        while (true){
            try {
               ItemVo<Order> itemVo =(ItemVo<Order>) delayQueue.take();
                Order order = (Order) itemVo.getObject();
                System.out.println(order.toString()+"已过期关闭订单。。。");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
package cn.bikan8;


import java.util.concurrent.*;

/**
 * @author lenovo
 */
public class Main {

    public static void main(String[] args) throws InterruptedException {
        DelayQueue delayQueue = new DelayQueue();
        new Thread(new PutOrder(delayQueue,2L,new Order("BIKANBA1","订单001"))).start();
        new Thread(new PutOrder(delayQueue,4L,new Order("BIKANBA2","订单002"))).start();
        new Thread(new RemoveOrder(delayQueue)).start();
        for (int i = 0;i<4;i++){
            Thread.sleep(1000);
            System.out.println("当前系统时间"+(i+1)+"s");
        }



    }
}

 

输出结果:

© 版权声明
THE END
喜欢就支持以下吧
点赞0
分享
评论 抢沙发