DelayQueue使用

  1. 使用延迟队列的元素必须实现Delayed接口
public class Task implements Delayed {
    /**
     * 到期时间,单位是秒
     */
    private final long timeOut;
    /**
     * 问题对象
     */
    private final Question question;
    /**
     * 产生序列号
     */
    private static final AtomicLong atomic = new AtomicLong(0);
    /**
     * 序列号
     */
    private final long sequenceNum;

    public Task(long timeout, Question question) {
        this.timeOut = System.nanoTime() + TimeUnit.NANOSECONDS.convert(timeout, TimeUnit.SECONDS);
        this.question = question;
        this.sequenceNum = atomic.getAndIncrement();
    }

    /**
     * 返回与此对象相关的剩余延迟时间,以给定的时间单位表示
     */
    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.timeOut - System.nanoTime(), TimeUnit.NANOSECONDS);
    }

    /**
     * 元素的先后顺序
     * @param other
     * @return
     */
    @Override
    public int compareTo(Delayed other) {
        if (other == this) // compare zero ONLY if same object
            return 0;
        if (other instanceof Task) {
            Task x = (Task) other;
            long diff = timeOut - x.timeOut;
            if (diff < 0)
                return -1;
            else if (diff > 0)
                return 1;
            else if (sequenceNum < x.sequenceNum)
                return -1;
            else
                return 1;
        }
        long d = (getDelay(TimeUnit.NANOSECONDS) - other.getDelay(TimeUnit.NANOSECONDS));
        return (d == 0) ? 0 : ((d < 0) ? -1 : 1);
    }

    public Question getQuestion() {
        return question;
    }
}
  1. 任务对象
public class Question {
    private boolean resloved;
    private String number;

    public Question(boolean resloved, String number) {
        this.resloved = resloved;
        this.number = number;
    }

    public void setResloved(boolean resloved) {
        this.resloved = resloved;
    }

    @Override
    public String toString() {
        return "Question{" +
                "resloved=" + resloved +
                ", number='" + number + '\'' +
                '}';
    }
}
  1. 执行任务
import java.util.concurrent.DelayQueue;

/**
 * Created by Administrator on 2017/5/26.
 */
public class Job {

    private Thread daemonThread;

    private DelayQueue delayQueue = new DelayQueue();

    public Job(){
        Runnable daemonTask = new DaemonThread();
        daemonThread = new Thread(daemonTask);
        daemonThread.setName("Cache Daemon");
        daemonThread.start();
    }

    //执行线程
    class DaemonThread implements Runnable{
        @Override
        public void run() {
            execute();
        }
    }
    /**
     * 添加任务,
     * time 延迟时间,时间是秒
     * q 问题
     * 用户为问题设置延迟时间
     */
    public void put(long time,Question q){
        //创建一个任务
        Task k = new Task(time,q);
        //将任务放在延迟的队列中
        delayQueue.put(k);
    }

    public void execute(){
        System.out.println("start");
        while(true) {
            try {
                //从延迟队列中取值,如果没有对象过期则队列一直等待,
                Task t1 = delayQueue.poll();
                if (t1 != null) {
                    //修改问题的状态
                    Question q = t1.getQuestion();
                    System.out.println("修改前"+q);
                    q.setResloved(true);
                    System.out.println("修改后"+q);
                }
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
        }
    }

    public static void main(String[] args) {
        Job job = new Job();
        Question question1 = new Question(false,"00888");

        job.put(5,question1);
        for (int i = 0; i < 10; i++){
            Question question = new Question(false,"00"+i);
            job.put(5,question);
        }
    }
}

你可能感兴趣的:(并发编程)