package J20250804.demo02;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author WanJl
 * @version 1.0
 * @title Producer
 * @description
 * @create 2025/8/4
 */
public class Producer implements Runnable{
    private String inputFile;
    private BlockingQueue<String> queue;

    public Producer(String inputFile, BlockingQueue<String> queue) {
        this.inputFile = inputFile;
        this.queue=queue;
    }

    @Override
    public void run() {
        try(BufferedReader br=new BufferedReader(new FileReader(inputFile))) {
            String line;
            //读取文件的每一行并且放入到队列中
            while ((line=br.readLine())!=null){
                try{
                    //如果队列满了，会阻塞等待
                    queue.put(line.trim());
                }catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    return;
                }
            }
        } catch (IOException e) {
            Thread.currentThread().interrupt();
        }
    }
}
