All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.huanminabc.utils_common.multithreading.queue.delay.ConsumerDelayed Maven / Gradle / Ivy

There is a newer version: 1.0.5-RELEASE
Show newest version
package com.gitee.huanminabc.utils_common.multithreading.queue.delay;

import com.gitee.huanminabc.utils_common.base.UniversalException;

import java.util.concurrent.DelayQueue;
import java.util.function.Consumer;

public class ConsumerDelayed implements Runnable {
    // 延时队列 ,消费者从其中获取消息进行消费
    private DelayQueue queue;
    private Consumer> consumer;

    public ConsumerDelayed(DelayQueue queue,Consumer> consumer) {
        this.queue = queue;
        this.consumer=consumer;
    }

    @Override
    public void run() {
        while (true) {
            try {
                if (queue.size()>0) {
                    //取出到期的元素,并且从队列中删除
                    MessageDelayed take = queue.take();
                    consumer.accept(take);
                }
            } catch (InterruptedException e) {
                 UniversalException.logError(e);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy