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

top.jfunc.common.thread.conpro.AbstractConsumer Maven / Gradle / Ivy

There is a newer version: 1.8.5
Show newest version
package top.jfunc.common.thread.conpro;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.BlockingQueue;

/**
 * @author xiongshiyan
 * 通用的消费者
 */
public abstract class AbstractConsumer implements Runnable , java.util.function.Consumer {
    private final BlockingQueue blockingQueue;

    private static Logger logger                      = LoggerFactory.getLogger(AbstractConsumer.class);

    /**
     * 构造方法传入与生产者相同的 BlockingQueue
     * @param blockingQueue 阻塞队列
     */
    public AbstractConsumer(BlockingQueue blockingQueue){
        this.blockingQueue = blockingQueue;
    }


    @Override
    public void run() {

        while (true){
            try {
                T take = blockingQueue.take();
                accept(take);
            } catch (InterruptedException e) {
                logger.error(e.getMessage() , e);
            }
        }
    }

    /**
     * 消费者去消费
     * @param t 消费的数据
     */
    @Override
    abstract public void accept(T t);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy