ArrayBlockingQueue使用

package org.example;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * 用数组实现的阻塞队列
 */
public class ArrayBlockingQueueDemo {
    ArrayBlockingQueue queue = new ArrayBlockingQueue(3);
    volatile boolean isStop = false;

    public static void main(String[] args) {
        ArrayBlockingQueueDemo demo = new ArrayBlockingQueueDemo();
        demo.test();
    }

    private void test() {
        List<Thread> producerThreadList = new ArrayList<>();
        List<Thread> consumerThreadList = new ArrayList<>();
        for (int j = 0; j < 3; j++) {
            producerThreadList.add((new Thread(this::producer, "包⼦⽣产商_" + j)));
        }
        for (int j = 0; j < 2; j++) {
            consumerThreadList.add((new Thread(this::consumer, "消息者_" + j)));
        }
        producerThreadList.forEach(Thread::start);
        consumerThreadList.forEach(Thread::start);

        producerThreadList.forEach(this::threadJoin);
        isStop = true;
    }

    private void producer() {
        try {
            for (int j = 0; j < 4; j++) {

                System.out.println(Thread.currentThread().getName() + ", ⽣产包⼦_" + j);
                queue.put("包⼦_" + j);
                Thread.sleep(200);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void consumer() {
        try {
            while (!isStop) {

                System.err.println(Thread.currentThread().getName() + ", 买到包⼦:" + queue.take());
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void threadJoin(Thread thread) {
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

改造后:

package org.example;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * 用数组实现的阻塞队列
 *
 * 如果消费者在前,生产者在后,就会出现消费者线程已经退出,又生产了几个包子,然后没有线程消费
 * 主要是讲解阻塞队列的使用情况,其实你可以发现,在案例中,生产和消费基本上是持平的,没有出现疯狂生产而消费跟不上的情况。
 * 具体说生产了几个,消费了几个就无所谓了
 * 可以理解为阻塞队列的加入,协调了生产和消费,相当于做了限流处理
 */
public class ArrayBlockingQueueDemo1 {
    ArrayBlockingQueue queue = new ArrayBlockingQueue(3);
    volatile boolean isStop = false;

    public static void main(String[] args) throws InterruptedException {
        ArrayBlockingQueueDemo1 demo = new ArrayBlockingQueueDemo1();
        demo.test();
    }

    private void test() throws InterruptedException {
        List<Thread> producerThreadList = new ArrayList<>();
        List<Thread> consumerThreadList = new ArrayList<>();
        for (int j = 0; j < 3; j++) {
            int finalJ = j;
            producerThreadList.add((new Thread(() -> {
                producer(finalJ); // finalJ
            }, "包⼦⽣产商_" + j)));
        }
        for (int j = 0; j < 2; j++) {
            consumerThreadList.add((new Thread(this::consumer, "消息者_" + j)));
        }
        producerThreadList.forEach(Thread::start);
        consumerThreadList.forEach(Thread::start);

        Thread.sleep(50000);

        producerThreadList.forEach(this::threadJoin);
        isStop = true;
    }

    private void producer(int flag) {
        try {
            for (int j = 0; j < 4; j++) {

                System.out.println(Thread.currentThread().getName() + ", ⽣产包⼦_" + j);
                queue.put("包⼦_" + flag + "=" + j);
                Thread.sleep(200);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void consumer() {
        try {
            while (!isStop) {

                System.err.println(Thread.currentThread().getName() + ", 买到包⼦:" + queue.take());
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void threadJoin(Thread thread) {
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

两者运行结果对比:

posted on   ~码铃薯~  阅读(12)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示