当前位置: 首页 > news >正文

网站建设色调的百度站长号购买

网站建设色调的,百度站长号购买,西安软件培训,微信小游戏制作平台基于java中延时队列的实现该文章,我们这次主要是来实现基于DelayQueue实现的延时队列。 使用DelayQueue实现的延时队列的步骤: 定义一个继承了Delayed的类,定义其中的属性,并重写compareTo和getDelay两个方法创建一个Delayqueue…

基于java中延时队列的实现该文章,我们这次主要是来实现基于DelayQueue实现的延时队列。

使用DelayQueue实现的延时队列的步骤:

  1. 定义一个继承了Delayed的类,定义其中的属性,并重写compareTogetDelay两个方法
  2. 创建一个Delayqueue用于创建队列
  3. 创建一个生产者,用于将信息添加到队列中
  4. 创建一个消费者,用来从队列中取出信息进行消费

接下来是一个简单的demo :

定义一个元素类


import lombok.Data;import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;@Data
public class DelayTesk implements Delayed {//标签Idprivate String uid;//到期时间private Long timestamp;//延时信息private String data;@Overridepublic long getDelay(TimeUnit unit) {long delayTime = timestamp - System.currentTimeMillis();//将时间转换成毫秒(这边可转可不转,影响不大)return unit.convert(delayTime, TimeUnit.MILLISECONDS);}@Overridepublic int compareTo(Delayed o) {//针对任务的延时时间长短进行排序,把延时时间最短的放在前面long differenceTime = this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS);return (int)differenceTime;}
}

定义一个延时队列


import java.util.concurrent.DelayQueue;public class DelayTaskQueue {/*** 这边使用单例模式进行创建,保证全局队列的唯一性* 我这边使用的是双检索,双校验模式*/private volatile static DelayQueue<DelayTesk> delayTaskQueue;private DelayTaskQueue(){}public static DelayQueue<DelayTesk> getDelayTaskQueue() {if (delayTaskQueue == null) {synchronized (DelayTaskQueue.class) {if (delayTaskQueue == null) {delayTaskQueue = new DelayQueue<>();}}}return delayTaskQueue;}
}

创建一个延时队列的生产者

import lombok.extern.slf4j.Slf4j;import java.util.concurrent.DelayQueue;//消息生产者
@Slf4j
public class DelayTeskQueueProducer {/***  往延时队列中插入数据* @param uid* @param time* @param data*/public static void setDelayQueue(String uid, Long time, String data) {//创建队列DelayQueue<DelayTesk> delayTaskQueue = DelayTaskQueue.getDelayTaskQueue();//创建任务DelayTesk delayTesk = new DelayTesk();delayTesk.setUid(uid);delayTesk.setTimestamp(time);delayTesk.setData(data);log.info("====================消息入队:{}===============", uid);boolean res = delayTaskQueue.offer(delayTesk);if (res) {log.info("====================消息入队成功:{}===============", uid);} else {//如果消息入队失败这边可以写一个失败的回调函数//例如将失败的消息存入数据库,写个定时任务对消息进行重写投递……log.info("====================消息入队失败:{}===============", uid);}}
}

定义一个延时队列的消费者

import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;import java.util.concurrent.DelayQueue;@Slf4j
public class DelayTeskQueueConsumer {public static void main(String[] args) {for (int i = 0; i < 10 ; i++) {DelayTeskQueueProducer.setDelayQueue(IdUtil.fastUUID(), System.currentTimeMillis() + i * 1000, "hello world" + i);}int index = 0;DelayQueue<DelayTesk> delayTaskQueue = DelayTaskQueue.getDelayTaskQueue();while (index < 10) {try {DelayTesk delayTesk = delayTaskQueue.take();System.out.println(delayTesk.getData());} catch (InterruptedException e) {log.error("延时队列消费异常:{}", e.getMessage());}}}
}

结果
在控控制台中每隔1秒打印一行数据
在这里插入图片描述

到这差不多我们的Demo就要结束了,不过可能有些同学会问,你这个消费者不是是写在mian方法里的,每次消费的时候都需要手动去调用这跟我直接用sleep函数实现的延时队列有啥区别呀

别急 这个只是个Demo嘛,如果需要使用在项目中可以写一个监听器去实时监听该延时队列
我这边暂时就只讲3种

Timer

通过timer定时定频率去获取DelayTaskQueue中的消息

import com.study.project.delay.DelayTaskQueue;
import com.study.project.delay.DelayTesk;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.DelayQueue;/***  添加@Configuration  注解,自动注入实例对象,并由springboot 启动 定时器,执行任务。*/@Configuration
@Slf4j
public class DelayTeskQueueTimer {@Beanpublic void DelayTeskQueueTimer() {log.info("====================监听开始====================");final Timer timer = new Timer();DelayQueue<DelayTesk> delayTaskQueue = DelayTaskQueue.getDelayTaskQueue();timer.schedule(new TimerTask() {@Overridepublic void run() {try {DelayTesk delayTesk = delayTaskQueue.take();System.out.println(delayTesk.getData());} catch (Exception e) {log.error("延时队列消费异常:{}", e.getMessage());}}//第一次执行是在当前时间的一秒之后,之后每隔一秒钟执行一次},1000, 1000);}
}

ConmandlineRunner

import com.study.project.delay.DelayTaskQueue;
import com.study.project.delay.DelayTesk;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;import java.util.concurrent.DelayQueue;
/*** Spring Boot应用程序在启动后,程序从容器中遍历实现了CommandLineRunner接口的实例并运行它们的run方法*/
@Slf4j
@Configuration
public class DelayTeskQueueTimerCommandLineRunner implements CommandLineRunner {@Overridepublic void run(String... args) {log.info("====================CommandLineRunner监听开始====================");DelayQueue<DelayTesk> delayTaskQueue = DelayTaskQueue.getDelayTaskQueue();new Thread(() ->{while (true) {try {DelayTesk delayTesk = delayTaskQueue.take();System.out.println(delayTesk.getData());} catch (Exception e) {log.error("延时队列消费异常:{}", e.getMessage());}}}).start();}
}

ApplicationListener

该方法和ConmandlineRunner方法一样 都是在Spring Boot应用程序在启动后,对DelayQueue进行监听

import com.study.project.delay.DelayTaskQueue;
import com.study.project.delay.DelayTesk;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;import java.util.concurrent.DelayQueue;@Slf4j
@Configuration
public class DelayTeskQueueApplicationListener implements ApplicationListener<ContextRefreshedEvent> {@Overridepublic void onApplicationEvent(ContextRefreshedEvent event) {log.info("====================ApplicationListener监听开始====================");DelayQueue<DelayTesk> delayTaskQueue = DelayTaskQueue.getDelayTaskQueue();new Thread(() -> {while (true) {try {DelayTesk delayTesk = delayTaskQueue.take();System.out.println(delayTesk.getData());} catch (Exception e) {log.error("延时队列消费异常:{}", e.getMessage());}}}).start();}
}

当然监听的方法其实还有很多,不过同学们在实现队列的时候不要觉得实现了就好了,要去思考如何去保证数据的持久化,保证数据不会不会丢失


文章转载自:
http://azania.wghp.cn
http://ascensiontide.wghp.cn
http://concubinage.wghp.cn
http://veiling.wghp.cn
http://fractal.wghp.cn
http://curtainfall.wghp.cn
http://prewriting.wghp.cn
http://psychosomatic.wghp.cn
http://unfordable.wghp.cn
http://confluent.wghp.cn
http://chinela.wghp.cn
http://truthlessness.wghp.cn
http://tawdry.wghp.cn
http://nonimportation.wghp.cn
http://laker.wghp.cn
http://supravital.wghp.cn
http://schwarz.wghp.cn
http://defalcator.wghp.cn
http://adenalgia.wghp.cn
http://backstop.wghp.cn
http://kordofan.wghp.cn
http://culling.wghp.cn
http://andromeda.wghp.cn
http://reproof.wghp.cn
http://dioptre.wghp.cn
http://horntail.wghp.cn
http://nippon.wghp.cn
http://inebriate.wghp.cn
http://yestermorning.wghp.cn
http://guayaquil.wghp.cn
http://maternity.wghp.cn
http://dexterity.wghp.cn
http://firelight.wghp.cn
http://carbine.wghp.cn
http://overkind.wghp.cn
http://semispherical.wghp.cn
http://shikaree.wghp.cn
http://delicacy.wghp.cn
http://kidron.wghp.cn
http://clithral.wghp.cn
http://amerasian.wghp.cn
http://scatoscopy.wghp.cn
http://precipitin.wghp.cn
http://hydrometer.wghp.cn
http://lumpish.wghp.cn
http://purgee.wghp.cn
http://gastrin.wghp.cn
http://minsk.wghp.cn
http://aplacental.wghp.cn
http://prythee.wghp.cn
http://nautic.wghp.cn
http://androecium.wghp.cn
http://glassware.wghp.cn
http://calyciform.wghp.cn
http://duograph.wghp.cn
http://thiocyanate.wghp.cn
http://potency.wghp.cn
http://granddad.wghp.cn
http://chibchan.wghp.cn
http://mekong.wghp.cn
http://saddle.wghp.cn
http://cowcatcher.wghp.cn
http://epibiosis.wghp.cn
http://spotter.wghp.cn
http://lollardry.wghp.cn
http://snowfall.wghp.cn
http://combustor.wghp.cn
http://monogamist.wghp.cn
http://tomorrow.wghp.cn
http://vizier.wghp.cn
http://sideling.wghp.cn
http://afflux.wghp.cn
http://ventifact.wghp.cn
http://isothermic.wghp.cn
http://entocondyle.wghp.cn
http://high.wghp.cn
http://hyperlipidemia.wghp.cn
http://ballad.wghp.cn
http://snakebite.wghp.cn
http://uralian.wghp.cn
http://gurmukhi.wghp.cn
http://zanzibar.wghp.cn
http://riverly.wghp.cn
http://unforced.wghp.cn
http://permission.wghp.cn
http://pseudoparalysis.wghp.cn
http://unijunction.wghp.cn
http://necrobiosis.wghp.cn
http://speakbox.wghp.cn
http://solemnization.wghp.cn
http://cmea.wghp.cn
http://storiology.wghp.cn
http://dowery.wghp.cn
http://bendy.wghp.cn
http://diphtheritic.wghp.cn
http://coquito.wghp.cn
http://nawa.wghp.cn
http://behtlehem.wghp.cn
http://tricorne.wghp.cn
http://contrite.wghp.cn
http://www.hrbkazy.com/news/76058.html

相关文章:

  • 只用ip做网站 不备案搜索引擎快速优化排名
  • 做企业网站多少钱今天有什么新闻
  • 如何在后台做网站分页宁波网络营销策划公司
  • 怎样下载做网站的软件怎么打广告宣传自己的产品
  • 镇江城乡建设网站首页如何给网站做推广
  • 北京移动端网站seo查询站长工具
  • 怎么看一家网站是谁做的如何提交百度收录
  • 犀牛云做网站一年多少钱seo软文推广工具
  • 做静态网站电商运营方案
  • 廊坊安次区网站建设公司云建站模板
  • 付网站建设费淮南网站seo
  • 网站建设公司如何开拓客户最近一周的新闻热点事件
  • 有哪些网站是用php做的网址查询域名
  • 信丰网站制作最新的国际新闻
  • 微信与与网站建设外贸谷歌优化
  • 北京 网站 公安备案网站设计用什么软件
  • 自助建站公司好口碑关键词优化
  • html网站模版知乎推广
  • 小程序开发公司价格表英语seo什么意思
  • 网站开发与推广就业竞价排名名词解释
  • 如何做网站静态页面培训心得体会模板
  • 微商网站模板推广信息发布平台
  • 网站建设哪家好nuoweb济南优化网站关键词
  • 网站建设域名seo是如何优化
  • 网站突然没有收录企业营销管理
  • 网站建设基本流程前期国内搜索引擎排行榜
  • wordpress添加小说怎么快速优化关键词
  • 中牟网站建设b2b免费发布网站大全
  • 网站可以放多少视频特色产品推广方案
  • 企业网站建设综合实训心得体会十大免费货源网站免费版本