基于 redis实现含有冻结时间的mq队列

来源:互联网 发布:字符能遍历吗python 编辑:程序博客网 时间:2024/06/09 21:38
redis自定义延时-mq.vsdx一、核心API

java延时队列  Delayed 
public interface RedisDelayed extends Delayed {
    public String toMessage();
}
自定义 java线程池  CountableThreadPool
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
原创粉丝点击