阻塞队列之DelayQueue

阻塞队列之DelayQueue

DelayQueue是一个支持延时获取元素的无界阻塞队列,在队列底层使用priorityqueue实现,DelayQueue队列中的元素必须实现delayed接口,该接口定义了在创建元素时该元素的延迟时间,在内部通过为每个元素的操作加锁来保障数据的一致性,只有在延迟时间到后才能从队列中提取元素,我们可以将DelayQueue运用以下场景中
缓存系统的设计:可以用DelayQueue保存缓存的有效性,使用一个线程循环查询DelayQueue,一旦能从DelayQueue中获取元素,则表示缓存的有效期到了
定时任务调度:使用DelayQueue保存即将执行的任务和执行时间,一旦从DelayQueue中获取元素,则表示任务开始执行,java中timerqueue就是通过DelayQueue是实现的

下面我们看下demo

package com.liu.demo09;

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

/**
 * @outhor liu
 * @dare 2020/7/14 16:34
 */
public class CDelayQueue implements Delayed {

    private int id;
    //消息内容
    private String body;
    //延迟时间
    private long Time;

    public CDelayQueue(int id, String body, long time) {
        this.id = id;
        this.body = body;
        this.Time = TimeUnit.NANOSECONDS.convert(time,TimeUnit.MILLISECONDS)+System.nanoTime();
    }

    public int getId() {

        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public long getTime() {
        return Time;
    }

    public void setTime(long time) {
        Time = time;
    }


    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.Time - System.nanoTime(),TimeUnit.NANOSECONDS);
    }

    //自定义方法
    @Override
    public int compareTo(Delayed o) {
        CDelayQueue c = (CDelayQueue)o;
        return Integer.valueOf(this.id)>Integer.valueOf(c.id)? 1
                :(Integer.valueOf(this.id)<Integer.valueOf(c.id)? -1 :0);
    }
}

package com.liu.demo09;

import java.util.concurrent.DelayQueue;

/**
 * @outhor liu
 * @dare 2020/7/14 17:02
 */
public class Consumer implements Runnable {

    //设置延时队列,消费者从其中获取消息进行消费
    private DelayQueue<CDelayQueue> queues;

    public Consumer(DelayQueue<CDelayQueue> queues) {
        this.queues = queues;
    }

    @Override
    public void run() {
        while (true){
            try {
                CDelayQueue take = queues.take();
                System.out.println("消费者id:" + take.getId() + "消费体:"+ take.getBody());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

package com.liu.demo09;

import java.util.concurrent.DelayQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @outhor liu
 * @dare 2020/7/14 16:39
 */
public class CTest {
    public static void main(String[] args) {
        //创建延时队列
        DelayQueue<CDelayQueue> cd = new DelayQueue<>();
        //添加延时消息,3s
        CDelayQueue w = new CDelayQueue(1, "world", 3000);
        //添加延时消息,5s
        CDelayQueue h = new CDelayQueue(2, "hello", 5000);
        //将延时消息放入队列中
        cd.offer(h);
        cd.offer(w);
        //启动消费线程
        ExecutorService e = Executors.newFixedThreadPool(1);
        e.execute(new Consumer(cd));
        e.shutdown();
    }
}

输出:

消费者id:1消费体:world
消费者id:2消费体:hello
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
探索全栈前端技术的魅力:HTML+CSS+JS+JQ+Bootstrap网站源码深度解析 在这个数字化时代,构建一个既美观又功能强大的网站成为了许多开发者和企业追逐的目标。本份资源精心汇集了一套完整网站源码,融合了HTML的骨架搭建、CSS的视觉美化、JavaScript的交互逻辑、jQuery的高效操作以及Bootstrap的响应式设计,全方位揭秘了现代网页开发的精髓。 HTML,作为网页的基础,它构建了信息的框架;CSS则赋予网页生动的外观,让设计创意跃然屏上;JavaScript的加入,使网站拥有了灵动的交互体验;jQuery,作为JavaScript的强力辅助,简化了DOM操作与事件处理,让编码更为高效;而Bootstrap的融入,则确保了网站在不同设备上的完美呈现,响应式设计让访问无界限。 通过这份源码,你将: 学习如何高效组织HTML结构,提升页面加载速度与SEO友好度; 掌握CSS高级技巧,如Flexbox与Grid布局,打造适应各种屏幕的视觉盛宴; 理解JavaScript核心概念,动手实现动画、表单验证等动态效果; 利用jQuery插件快速增强用户体验,实现滑动效果、Ajax请求等; 深入Bootstrap框架,掌握移动优先的开发策略,响应式设计信手拈来。 无论是前端开发新手渴望系统学习,还是资深开发者寻求灵感与实用技巧,这份资源都是不可多得的宝藏。立即深入了解,开启你的全栈前端探索之旅,让每一个网页都成为技术与艺术的完美融合!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

计算机毕业设计,

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值