利用BlockingQueue实现生产者消费者模式

本文介绍了Java并发包中的BlockingQueue,如ArrayBlockingQueue、LinkedBlockingQueue等,展示了如何在多线程环境下使用它们实现生产者消费者模型,并提供了代码实例。
摘要由CSDN通过智能技术生成

利用BlockingQueue实现生产者消费者模式

一、BlockingQueue介绍

BlockingQueue是 java.util.concurrent 包提供的用于解决并发生产者 - 消费者问题的类。在往队列中添加或者取出元素时通过ReentrantLock实现线程安全。常见的阻塞队列有ArrayBlockingQueue (由数组支持的有界队列)、LinkedBlockingQueue (由链接节点支持的可选有界队列)、PriorityBlockingQueue (由优先级堆支持的无界优先级队列)、DelayQueue (由优先级堆支持的、基于时间的调度队列)

二、多线程生产者消费者示例

1.定义生产者者

class Producer implements Runnable {
        private volatile boolean isRunning = true;
        private String name;

        public Producer(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            try {
                String deviceId = "cs.001";
                String subDeviceId = deviceId + COLLECTION_BOARDS2[0];
                while (isRunning) {
                    DevImsi data = imsiBuilder.buildImsi(deviceId, subDeviceId);
                    if (!blockingQueue.offer(data, 2, TimeUnit.SECONDS)) {
                        System.out.println("failed to put data into queue: " + data);
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                isRunning = false;
            } finally {
                System.out.println("Thread: " + this.name + " quit from producer thread");
            }

        }

        public void stop() {
            isRunning = false;
        }
    }

2.定义消费者

class Consumer implements Runnable {
        private volatile boolean isRunning = true;
        private String name;

        public Consumer(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            try {
                while (isRunning) {
                    DevImsi data = null;
                    data = blockingQueue.poll(2, TimeUnit.SECONDS);
                    System.out.println(data);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                isRunning = false;
            } finally {
                System.out.println("Thread: " + this.name + " quit from consumer thread");
            }
        }

        public void stop() {
            isRunning = false;
        }
    }

3.测试

    public static void main(String[] args) throws InterruptedException {
        Producer producer1 = new Producer("producer1");
        Producer producer2= new Producer("producer2");
        Producer producer3= new Producer("producer3");
        Consumer consumer1 = new Consumer("consumer1");
        Consumer consumer2 = new Consumer("consumer2");
        ExecutorService service = Executors.newCachedThreadPool();
        service.execute(producer1);
        service.execute(producer2);
        service.execute(producer3);
        service.execute(consumer1);
        service.execute(consumer2);

        TimeUnit.SECONDS.sleep(3);
        producer1.stop(); // 一定要先关闭生产者
        producer2.stop();
        producer3.stop();
        consumer1.stop();
        consumer2.stop();

        TimeUnit.SECONDS.sleep(2);
        service.shutdown();
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值