package cn.reghao.tnb.content.app.vod.service.rcmd.task;

import org.springframework.stereotype.Component;

import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author reghao
 * @date 2024-12-22 21:19:48
 */
@Component
public class RcmdProducer {
    private final Object monitor;
    private final LinkedBlockingQueue<Object> dataQueue;

    public RcmdProducer() {
        this.monitor = new Object();
        this.dataQueue = new LinkedBlockingQueue<>(10_000);
    }

    public Object getMonitor() {
        return monitor;
    }

    public void put(Object object) throws InterruptedException {
        dataQueue.put(object);
        synchronized (monitor) {
            // 通知 consumer 线程有数据可用
            monitor.notify();
        }
    }

    public Object get() throws InterruptedException {
        return dataQueue.poll();
        //return dataQueue.take();
    }

    public int size() {
        return dataQueue.size();
    }
}
