【学习任务】实现一个生产者消费者模式
我要参与
【学习任务】实现一个生产者消费者模式
学习任务 422
等3人参与
来源: 第7周 / 移动端架构师

【实践描述】

使用ReentrantLock,实现一个生产者消费者模式

【任务要求】

  • 构建一个生产者,每隔1s生产出一块面包

  • 构建两个消费者,消费者A每隔2s消费一块面包,消费者B3s消费一个面包

【思路点拨】

  • 使用ReentrantLock.codition条件唤醒

  • 每次执行任务时都需要拿到condition对象

  • 生产完记得唤醒消费者



去发布

登录后即可发布作业,立即

我的作业

全部作业 3

慕莱坞42732

public class ProducerConsumerExample {
private volatile List buffer = new ArrayList<>();
private final ReentrantLock lock = new ReentrantLock(true);
private final Condition work1 = lock.newCondition();
private final Condition work2 = lock.newCondition();

public void produce() {
    try {
        lock.lock();
        System.out.println("生产者加锁");
        buffer.add(new Random().nextInt(100));
        System.out.println("生产池数量-->" + buffer.size());
        System.out.println("通知两个消费者");
        work1.signal();
        work2.signal();
    } finally {
        lock.unlock();
        System.out.println("生产者释放锁");
    }
}

public void consume() {
    try {
        lock.lock();
        System.out.println("消费者1加锁");
        if (buffer.isEmpty()) {
            System.out.println("生产池已经空了");
            work1.await();
        } else {
            buffer.remove(0);
            System.out.println("消费者1----生产池数量-->" + buffer.size());
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        lock.unlock();
        System.out.println("消费者1释放锁");
    }
}

public void consume2() {
    try {
        lock.lock();
        System.out.println("消费者2加锁");
        if (buffer.isEmpty()) {
            System.out.println("生产池已经空了");
            work2.await();
        } else {
            buffer.remove(0);
            System.out.println("消费者2----生产池数量-->" + buffer.size());
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        lock.unlock();
        System.out.println("消费者2释放锁");
    }
}

public static void main(String[] args) {
    ProducerConsumerExample example = new ProducerConsumerExample();
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                try {
                    example.consume();
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }).start();

    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                try {
                    example.consume2();
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }).start();

    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                try {
                    example.produce();
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

        }
    }).start();
}
  • 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
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
代码块
复制 预览
复制成功!

}

提交于  2024-07-18 14:37:41
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师