package com.yn.bftl.thirdparty.modules.secret.controller;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Test {

    // 创建一个容量为10的阻塞队列
    private static final BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(10);

    public static void main(String[] args) {
        // 创建生产者线程
        Thread producer = new Thread(() -> {
            for (int i = 0; i < 20; i++) {
                try {
                    // 生产元素并放入队列
                    System.out.println("Produced: " + i);
                    queue.put(i);
                    Thread.sleep(100); // 模拟耗时操作
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        });

        // 创建消费者线程
        Thread consumer = new Thread(() -> {
            while (true) {
                try {
                    // 从队列中取出元素
                    Integer take = queue.take();
                    System.out.println("Consumed: " + take);
                    if (take == 19) { // 假设我们知道生产了20个元素
                        break;
                    }
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    break;
                }
            }
        });

        // 启动线程
        producer.start();
        consumer.start();

        // 等待线程结束
        try {
            producer.join();
            consumer.join();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
