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

本文深入探讨了Java中ScheduledExecutorService接口的使用,包括创建周期任务、延迟执行和固定频率执行的方法。同时,通过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;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值