基于 redis实现含有冻结时间的mq队列
来源:互联网 发布:字符能遍历吗python 编辑:程序博客网 时间:2024/06/06 06:34
一、核心API
java延时队列 Delayed
public interface RedisDelayed extends Delayed {
public String toMessage();
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* 计数线程池
*/
public class CountableThreadPool {
/**
* 线程数
*/
private int threadNum;
/**
*
*
* 一个提供原子操作的Integer的类。
* 在Java语言中,++i和i++操作并不是线程安全的,在使用的时候,不可避免的会用到synchronized关键字。
* 而AtomicInteger则通过一种线程安全的加减操作接口。
**/
private AtomicInteger threadAlive = new AtomicInteger();
private ReentrantLock reentrantLock = new ReentrantLock();
private Condition condition = reentrantLock.newCondition();
private ExecutorService executorService;
/**
*
* @param threadNum
* 线程数
*/
public CountableThreadPool(int threadNum) {
this.threadNum = threadNum;
this.executorService = Executors.newFixedThreadPool(threadNum);
}
public CountableThreadPool(int threadNum, ExecutorService executorService) {
this.threadNum = threadNum;
this.executorService = executorService;
}
public void setExecutorService(ExecutorService executorService) {
this.executorService = executorService;
}
/**
* 活动线程数
*
* @return
*/
public int getThreadAlive() {
return threadAlive.get();
}
public int getThreadNum() {
return threadNum;
}
public void execute(final Runnable runnable) {
// 等待线程池中的名额
if (threadAlive.get() >= threadNum) {//当运行中的线程大于线程池设定的线程数时,进入等待状态。
try {
reentrantLock.lock();
while (threadAlive.get() >= threadNum) {//双层判定,你懂得
try {
condition.await();//进入等待状态,不继续执行 runnable内容
} catch (InterruptedException e) {
}
}
} finally {
reentrantLock.unlock();
}
}
threadAlive.incrementAndGet();//原子性递增
executorService.execute(new Runnable() {
public void run() {
try {
runnable.run();
} finally {
try {
reentrantLock.lock();
threadAlive.decrementAndGet();//原子性递递减
condition.signal();//唤醒第一个正在等待(被阻塞)的线程
} finally {
reentrantLock.unlock();
}
}
}
});
}
public boolean isShutdown() {
return executorService.isShutdown();
}
public void shutdown() {
executorService.shutdown();
}
}
java 延时队列 DelayQueue<T extends Delayed> () ,自定义延时队列的实现类,使用redis sortSet的api实现poll、add、iterator等操作。
redis api
redis zset 数据类型 zadd 、zrangeByScore
二、流程图
0 0
- 基于 redis实现含有冻结时间的mq队列
- GuozhongCrawler实现的基于redis的队列
- 基于Redis实现的延迟队列
- 使用jedis实现Redis消息队列(MQ)的发布(publish)和消息监听(subscribe)
- 基于Redis实现延迟队列
- 使用Redis实现MQ
- 基于redis+mysql+php的简单队列实现
- 基于Go实现的分布式MQ
- 基于Go实现的分布式MQ
- 基于redis的消息队列
- redis队列的实现
- redis队列的实现
- SpringBoot 基于Redis快速实现消息队列
- 2种基于简单定时任务实现分布式定时任务的技术解决方案(zookeeper、redis和内联MQ)
- Laravel 队列系列 —— 基于 Redis 实现任务队列的基本配置和使用
- Redis应用3-基于Redis消息队列实现的异步操作
- 消息队列mq的原理及实现方法
- 消息队列mq的原理及实现方法
- 行列元素 eg:img之间明明没距离,为什么还是有间距
- Android状态栏一体化 (改变状态栏的背景颜色)
- Kindeditor在JSP项目中的应用
- mysql学习总结2(高性能MySQL笔记)
- Ubuntu VPN (PPTP\L2TP\OpenVPN) server and client
- 基于 redis实现含有冻结时间的mq队列
- 电脑快捷键大全
- Sphinx在windows下安装使用[支持中文全文检索]
- Android学习历程3-倒计时类的简单介绍
- 串行FLASH数据缓冲区的管理
- RSync实现文件备份同步
- android annotations 和 微信分享功能学习笔记
- spring MVC 配置入门总结(1)
- 使用afinal 中的finalhttp和finalbitmap访问网络资源+finalActivity