首页 > Java > java教程 > 正文

在Java中如何实现生产者消费者模式

P粉602998670
发布: 2025-10-26 13:19:02
原创
583人浏览过
Java中实现生产者消费者模式的核心是通过线程间通信协调共享缓冲区的访问,常用方法有三种:1. 使用synchronized配合wait()和notify()实现基础同步;2. 使用BlockingQueue如ArrayBlockingQueue,利用其内置阻塞机制简化开发;3. 使用Lock与Condition提供更灵活的多条件等待控制。其中BlockingQueue最推荐用于实际开发,而wait/notify和Lock/Condition更适合理解底层原理或定制场景。

在java中如何实现生产者消费者模式

在Java中实现生产者消费者模式,核心是让生产者线程向共享缓冲区添加数据,消费者线程从中取出数据,同时保证线程安全和避免资源浪费。关键在于线程间的协调与通信,常用方式包括使用 wait()notify()BlockingQueueLockCondition。下面介绍几种典型实现方法。

使用 wait() 和 notify()

这是最基础的实现方式,依赖 synchronized 关键字控制同步,通过 wait() 让线程等待,notify() 唤醒等待线程。

定义一个共享的缓冲区(例如容量为1的队列):

class Buffer {
    private int data;
    private boolean isEmpty = true;

    public synchronized void put(int value) {
        while (!isEmpty) {
            try {
                wait(); // 缓冲区非空,生产者等待
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
        data = value;
        isEmpty = false;
        notify(); // 唤醒消费者
    }

    public synchronized int take() {
        while (isEmpty) {
            try {
                wait(); // 缓冲区为空,消费者等待
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
        isEmpty = true;
        notify(); // 唤醒生产者
        return data;
    }
}
登录后复制

生产者和消费者分别作为线程运行:

Buffer buffer = new Buffer();

Thread producer = new Thread(() -> {
    for (int i = 0; i < 5; i++) {
        buffer.put(i);
        System.out.println("生产: " + i);
    }
});

Thread consumer = new Thread(() -> {
    for (int i = 0; i < 5; i++) {
        int value = buffer.take();
        System.out.println("消费: " + value);
    }
});

producer.start();
consumer.start();
登录后复制

使用 BlockingQueue

更推荐的方式是使用 java.util.concurrent 包中的 BlockingQueue,它内部已实现线程安全和阻塞操作。

常见实现类有 ArrayBlockingQueue(有界队列)和 LinkedBlockingQueue(可选有界)。

歌者PPT
歌者PPT

歌者PPT,AI 写 PPT 永久免费

歌者PPT 197
查看详情 歌者PPT

立即学习Java免费学习笔记(深入)”;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ArrayBlockingQueue;

BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(10);

Thread producer = new Thread(() -> {
    for (int i = 0; i < 5; i++) {
        try {
            queue.put(i); // 队列满时自动阻塞
            System.out.println("生产: " + i);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
});

Thread consumer = new Thread(() -> {
    for (int i = 0; i < 5; i++) {
        try {
            Integer value = queue.take(); // 队列空时自动阻塞
            System.out.println("消费: " + value);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
});

producer.start();
consumer.start();
登录后复制

使用 Lock 和 Condition

这种方式比 synchronized 更灵活,可以创建多个条件变量,适用于复杂场景。
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.locks.*;

class BufferWithLock {
    private final Queue<Integer> queue = new LinkedList<>();
    private final int MAX_SIZE = 10;
    private final Lock lock = new ReentrantLock();
    private final Condition notFull = lock.newCondition();
    private final Condition notEmpty = lock.newCondition();

    public void put(int value) throws InterruptedException {
        lock.lock();
        try {
            while (queue.size() == MAX_SIZE) {
                notFull.await(); // 队列满,等待
            }
            queue.offer(value);
            notEmpty.signal(); // 唤醒消费者
        } finally {
            lock.unlock();
        }
    }

    public int take() throws InterruptedException {
        lock.lock();
        try {
            while (queue.isEmpty()) {
                notEmpty.await(); // 队列空,等待
            }
            int value = queue.poll();
            notFull.signal(); // 唤醒生产者
            return value;
        } finally {
            lock.unlock();
        }
    }
}
登录后复制

使用方式与前面类似,创建线程调用 put 和 take 方法即可。

基本上就这些。BlockingQueue 是最简洁可靠的实现,适合大多数情况;而 wait/notify 和 Lock/Condition 更适合学习原理或定制逻辑。

以上就是在Java中如何实现生产者消费者模式的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号