使用java做订单失效(指定一段时间后)

本人所在的公司有个需求是订单为1小时有效期,过了有效期之后订单即置为失效,代码贴出来跟大家分享一下

import java.util.concurrent.TimeUnit;

public class OrderTest {
    public static void main(String[] args) {
        for(int i = 1;i <= 10;i++){
            final int id = i;
            new Thread(new Runnable(){
                @Override
                public void run() {
                    System.out.println(System.currentTimeMillis());
                    new Order(id,new ScheduleTime(3, TimeUnit.SECONDS),0);
                }
            }).start();
        }
    }
}

/**
 * 订单类
 * @author pipi
 *
 */
public class Order {
    private Integer id; //订单id
    private Integer status; //订单状态,0为未付款,1为已付款,-1为已失效
    private ScheduleTime time;
    public Order(Integer id,ScheduleTime time,Integer status){
        this.id = id;
        this.time = time;
        this.status = status;
        OrderSchedule.getInstance().startTask(new OrderFailure(this), time);
    }

    public boolean isAlive(){
        return status != -1;
    }

    public Integer getId() {
        return id;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }
}

import java.util.concurrent.TimeUnit;

public class ScheduleTime {
    private long delay;
    private TimeUnit time;
    public ScheduleTime(long delay,TimeUnit time){
        this.delay = delay;
        this.time = time;
    }
    public long getDelay() {
        return delay;
    }
    public void setDelay(long delay) {
        this.delay = delay;
    }
    public TimeUnit getTime() {
        return time;
    }
    public void setTime(TimeUnit time) {
        this.time = time;
    }

}

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * 调度器
 * @author pipi
 *
 */
public class OrderSchedule {
    private static ScheduledExecutorService Manager;
    private static OrderSchedule orderSchedule;

    static{
        Manager = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors());
    }

    private OrderSchedule(){
    }

    public synchronized static OrderSchedule getInstance(){
        if(orderSchedule == null){
            orderSchedule = new OrderSchedule();
        }
        return orderSchedule;
    }

    /**
     * 预约订单时调用,一段时间后订单失效
     */
    public void startTask(OrderFailure task,ScheduleTime time){
        Manager.schedule(task,time.getDelay(),time.getTime());
    }
}

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 订单失效操作
 * @author pipi
 *
 */
public class OrderFailure implements Runnable{
    private Order order;
    private static Lock lock = new ReentrantLock();
    public OrderFailure(Order order){
        this.order = order;
    }

    @Override
    public void run() {
        if(order.getStatus() == 1) System.out.println("订单已付款,不执行失效任务");
        else{
            lock.lock();
            try{
                order.setStatus(-1);
                System.out.println("id为" + order.getId() + "的订单,失效任务执行");
                System.out.println(System.currentTimeMillis());
            }finally{
                lock.unlock();
            }


        }
    }

    /**
     * 返回订单是否付款
     * @return
     */
    public boolean isPay(){
        if(order.getStatus() == 1) return true;
        else return false;
    }
}

你可能感兴趣的:(多线程,Java基础)