阻塞队列-DelayQueue实现周期任务

周期任务

ScheduledExecutorService

API

ScheduledExecutorService是已有的周期任务 实现,提供了

ScheduledExecutorService schedulePool= Executors.newScheduledThreadPool(2);
// 延迟5s后只执行一次
schedulePool.schedule(task,5,TimeUnit.SECONDS);
// 延迟0s后每5s周期执行
schedulePool.scheduleAtFixedRate(task,0,5, TimeUnit.SECONDS);

实现

根据DelayQueue,

DelayQueue

周期任务简易实现

package concurrency.synCollection;

import org.junit.Test;

import java.util.concurrent.*;

public class DelayQueueExp {

    private static int nthread = 5;
    DelayQueue<Task> delayQueue = new DelayQueue<>();
    ExecutorService executorService = Executors.newFixedThreadPool(5);
    @Test
    public void delayQueueTest() throws Exception{
        for (int i=0;i<nthread/2;i++) {
            // task 延迟1s后继续执行
            executorService.submit(new Task(1000L));
        }
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    // 从延迟队列中取出,重新提交执行
                    try {
                        Task task = delayQueue.take();
                        executorService.submit(task);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        Thread.currentThread().join();
    }

    class Task implements Delayed, Callable<Long> {

        // 延迟时长,出队时会判断是否经过了interval时间,没有的话阻塞等待
        private long interval;
        // 放入队列的时间,用来判断延迟时间是够达到
        private long time;
        // 由于底层是堆,所以需要实现Callable
        public int compareTo(Delayed e) {
            return (int)(this.time - ((Task)e).getTime());
        }
        // 返回剩余所需延迟时间,Delayed接口,当小于0时可以出队
        public long getDelay(TimeUnit unit) {
            return unit.convert((this.time + this.interval) - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
        }

        public Long call(){
            Long result = System.currentTimeMillis();
            System.out.println(Thread.currentThread().getName() + " curTime:" + result);
            time = System.currentTimeMillis();
            // 任务执行完后,加入延迟队列,等待延迟时间后下次执行
            delayQueue.offer(this);
            return result;
        }

        public Task (long interval) {
            this.interval = interval;
        }
        public long getTime() {
            return time;
        }
    }
}

你可能感兴趣的:(并发编程,BlockingQueue,周期任务,定时任务,schedule)