博客
关于我
生产者消费者 Java
阅读量:193 次
发布时间:2019-02-28

本文共 5948 字,大约阅读时间需要 19 分钟。

同步实现

在生产消费问题中,Java的synchronized关键字提供了一种简单且有效的方式来管理共享资源的访问。通过对对象本身进行锁定,确保在多线程环境下,同一时间只允许一个线程执行特定方法。这种机制避免了数据竞争和不一致性问题。

代码解析

public class App {    private int num;    public void product() {        try {            Thread.sleep(1000);            synchronized (this) {                while (num >= 100) {                    wait();                }                num++;                System.out.println("生产");                System.out.println("库存量:" + num);                notifyAll();            }        } catch (InterruptedException e) {            e.printStackTrace();        }    }    public void sell() {        try {            Thread.sleep(500);            synchronized (this) {                while (num <= 0) {                    wait();                }                num--;                System.out.println("卖出");                System.out.println("库存量:" + num);                notifyAll();            }        } catch (InterruptedException e) {            e.printStackTrace();        }    }    public static void main(String[] args) throws Exception {        App app = new App();        Runnable product = new Runnable() {            @Override            public void run() {                while (true) {                    app.product();                }            }        };        Runnable sell = new Runnable() {            @Override            public void run() {                while (true) {                    app.sell();                }            }        };        for (int i = 0; i < 5; i++) {            new Thread(product).start();        }        for (int i = 0; i < 10; i++) {            new Thread(sell).start();        }    }}

优点分析

  • 简单易用:通过锁定对象,确保单线程访问,避免数据竞争。
  • 内置调度wait()notify() 方法自动协调线程等待和唤醒。
  • 无需额外库:依赖Java的核心机制,无需额外依赖第三方库。

缺点

  • 性能限制synchronized的性能相对较低,特别是在高并发场景下。
  • 无法细粒度控制:只能锁定整个对象,难以实现细粒度的资源控制。

阻塞队列实现

阻塞队列是一种更高级的生产消费模式,通过自动管理资源的分配和等待,减少了生产消费问题中常见的死锁和不一致性问题。

代码解析

import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;import java.util.concurrent.atomic.AtomicInteger;public class App {    private BlockingQueue
blockingQueue = new ArrayBlockingQueue<>(100); private AtomicInteger atomicInteger = new AtomicInteger(); public void product() { try { Thread.sleep(1000); blockingQueue.offer(atomicInteger.incrementAndGet()); System.out.println("工厂" + Thread.currentThread().getName() + "生产\n库存量:" + blockingQueue.size()); } catch (InterruptedException e) { e.printStackTrace(); } } public void sell() { try { Thread.sleep(500); System.out.println("消费者" + Thread.currentThread().getName() + "购买" + blockingQueue.take() + "号商品\n库存量:" + blockingQueue.size()); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { App app = new App(); Runnable product = new Runnable() { @Override public void run() { while (true) { app.product(); } } }; Runnable sell = new Runnable() { @Override public void run() { while (true) { app.sell(); } } }; for (int i = 0; i < 3; i++) { new Thread(product).start(); } for (int i = 0; i < 5; i++) { new Thread(sell).start(); } }}

优点分析

  • 自动管理:队列自动管理资源的分配和等待,减少了手动协调的复杂性。
  • 减少死锁:通过自动等待和唤醒,避免了生产消费过程中可能出现的死锁问题。
  • 高效率:相比synchronized,阻塞队列在多线程场景下表现更为高效。

缺点

  • 资源占用:队列占用了内存空间,可能在资源有限的情况下造成压力。
  • 学习曲线:相比synchronized,阻塞队列的使用需要更深入的理解和掌握。

ReentrantLock实现

ReentrantLock是一种更高级的互斥机制,结合了传统锁和信号量的优点,支持多级重入,适用于复杂的多线程场景。

代码解析

import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;public class App {    private ReentrantLock lock = new ReentrantLock();    private final Condition notFull = lock.newCondition();    private final Condition notEmpty = lock.newCondition();    private int num = 0;    public void product() {        try {            Thread.sleep(1000);            lock.lock();            while (num > 100) {                notFull.await();            }            num++;            System.out.println("工厂" + Thread.currentThread().getName() + "生产\n库存:" + num);            notEmpty.signalAll();        } catch (Exception e) {            e.printStackTrace();        } finally {            lock.unlock();        }    }    public void sell() {        try {            Thread.sleep(800);            lock.lock();            while (num <= 0) {                notEmpty.await();            }            num--;            System.out.println("顾客" + Thread.currentThread().getName() + "购买\n库存:" + num);            notFull.signalAll();        } catch (Exception e) {            e.printStackTrace();        } finally {            lock.unlock();        }    }    public static void main(String[] args) {        App app = new App();        for (int i = 0; i < 10; i++) {            new Thread(new Runnable() {                @Override                public void run() {                    while (true) {                        app.product();                    }                }            }).start();        }        for (int i = 0; i < 5; i++) {            new Thread(new Runnable() {                @Override                public void run() {                    while (true) {                        app.sell();                    }                }            }).start();        }    }}

优点分析

  • 灵活性:支持多级重入,适用于需要多次获取锁的情况。
  • 细粒度控制:通过Condition对象,实现了对资源状态的细致监控。
  • 性能优化:相比synchronizedReentrantLock的性能更高,特别是在多线程高并发场景下。

缺点

  • 复杂性:相比synchronizedReentrantLock的使用稍微复杂,需要配置Condition对象。
  • 依赖库:需要引入java.util.concurrent.locks包中的类,增加了依赖性。

总结

在生产消费问题中,选择合适的锁机制至关重要。synchronized简单易用,但在高并发场景下性能不足;BlockingQueue提供了更高级的资源管理,但需要较高的学习成本;ReentrantLock则在性能和灵活性之间找到了平衡,适用于更复杂的多线程场景。根据具体需求选择合适的实现方案,可以最大化资源利用率和系统性能。

转载地址:http://upvj.baihongyu.com/

你可能感兴趣的文章
nginx + etcd 动态负载均衡实践(二)—— 组件安装
查看>>
nginx + etcd 动态负载均衡实践(四)—— 基于confd实现
查看>>
Nginx + Spring Boot 实现负载均衡
查看>>
Nginx + uWSGI + Flask + Vhost
查看>>
Nginx - Header详解
查看>>
Nginx Location配置总结
查看>>
Nginx Lua install
查看>>
Nginx upstream性能优化
查看>>
Nginx 中解决跨域问题
查看>>
Nginx 动静分离与负载均衡的实现
查看>>
Nginx 反向代理 MinIO 及 ruoyi-vue-pro 配置 MinIO 详解
查看>>
nginx 反向代理 转发请求时,有时好有时没反应,产生原因及解决
查看>>
Nginx 反向代理解决跨域问题
查看>>
Nginx 反向代理配置去除前缀
查看>>
nginx 后端获取真实ip
查看>>
Nginx 学习总结(16)—— 动静分离、压缩、缓存、黑白名单、性能等内容温习
查看>>
Nginx 学习总结(17)—— 8 个免费开源 Nginx 管理系统,轻松管理 Nginx 站点配置
查看>>
Nginx 常用配置清单
查看>>
nginx 常用配置记录
查看>>
Nginx 我们必须知道的那些事
查看>>