【J2SE】生成者、消费者问题
本文转自:《Java JDK 实例宝典》
解决生产者消费者问题的关键技术点如下:
1.用线程模拟生产者,在run方法中不断地往仓库中存放产品。
2.用线程模拟消费者,在run方法中不断地从仓库中获取产品。
3.仓库类保存产品,当产品数量为0时,调用wait方法,使得当前消费者线程进入等待状态,当有新产品存入时,调用nofify方法,唤醒等待的消费者线程。当仓库满时,调用wait方法,使得当前生产者线程进入等待状态,当有消费者获取产品时,调用notify方法,唤醒等待的生产者线程。
4.停止生产者和消费者线程都通过设置标志位running为false实现。
Product类:
Producer类:
TestProduct类:package com.syc.examples.chapter8;public class TestProduct {public static void main(String[] args){Warehouse warehouse = new Warehouse(10); // 建立一个仓库,容量为10// 建立生产者和消费者Producer producers1 = new Producer(warehouse,"producer-1");Producer producers2 = new Producer(warehouse,"producer-2");Producer producers3 = new Producer(warehouse,"producer-3");Consumer consumers1 = new Consumer(warehouse,"consumer-1");Consumer consumers2 = new Consumer(warehouse,"consumer-2");Consumer consumers3 = new Consumer(warehouse,"consumer-3");Consumer consumers4 = new Consumer(warehouse,"consumer-4");// 启动生产者和消费者线程producers1.start();producers2.start();consumers1.start();producers3.start();consumers2.start();consumers3.start();consumers4.start();// 让生产者消费者程序预习1600mstry {Thread.sleep(1600);} catch (InterruptedException e) {e.printStackTrace();}// 停止生产者和消费者的线程producers1.stopProducer();consumers1.stopConsumer();producers2.stopProducer();consumers2.stopConsumer();producers3.stopProducer();consumers3.stopConsumer();consumers4.stopConsumer();}}
引用Producer[producer-1]storageProduct:Product-1
仓库中还没有被消费的产品数量:1
Producer[producer-2]storageProduct:Product-2
仓库中还没有被消费的产品数量:2
Consumer[consumer-1] getProduct: Product-1
仓库中还没有被消费的产品数量:1
Producer[producer-3]storageProduct:Product-3
仓库中还没有被消费的产品数量:2
Consumer[consumer-2] getProduct: Product-2
仓库中还没有被消费的产品数量:1
Consumer[consumer-3] getProduct: Product-3
仓库中还没有被消费的产品数量:0
Producer[producer-1]storageProduct:Product-4
仓库中还没有被消费的产品数量:1
Consumer[consumer-4] getProduct: Product-4
仓库中还没有被消费的产品数量:0
Producer[producer-2]storageProduct:Product-5
仓库中还没有被消费的产品数量:1
Producer[producer-3]storageProduct:Product-6
仓库中还没有被消费的产品数量:2
Consumer[consumer-1] getProduct: Product-5
仓库中还没有被消费的产品数量:1
Consumer[consumer-2] getProduct: Product-6
仓库中还没有被消费的产品数量:0
Producer[producer-1]storageProduct:Product-7
仓库中还没有被消费的产品数量:1
Consumer[consumer-3] getProduct: Product-7
仓库中还没有被消费的产品数量:0
Producer[producer-2]storageProduct:Product-8
仓库中还没有被消费的产品数量:1
Producer[producer-3]storageProduct:Product-9
仓库中还没有被消费的产品数量:2
Consumer[consumer-4] getProduct: Product-8
仓库中还没有被消费的产品数量:1
Producer[producer-1]storageProduct:Product-10
仓库中还没有被消费的产品数量:2
Producer[producer-2]storageProduct:Product-11
仓库中还没有被消费的产品数量:3
Producer[producer-3]storageProduct:Product-12
仓库中还没有被消费的产品数量:4
Consumer[consumer-1] getProduct: Product-9
仓库中还没有被消费的产品数量:3
Consumer[consumer-2] getProduct: Product-10
仓库中还没有被消费的产品数量:2
Consumer[consumer-3] getProduct: Product-11
仓库中还没有被消费的产品数量:1
Producer[producer-1]storageProduct:Product-13
仓库中还没有被消费的产品数量:2
Producer[producer-2]storageProduct:Product-14
仓库中还没有被消费的产品数量:3
Producer[producer-3]storageProduct:Product-15
仓库中还没有被消费的产品数量:4
Consumer[consumer-4] getProduct: Product-12
仓库中还没有被消费的产品数量:3
Consumer[consumer-1] getProduct: Product-13
仓库中还没有被消费的产品数量:2
Consumer[consumer-2] getProduct: Product-14
仓库中还没有被消费的产品数量:1
Producer[producer-1]storageProduct:Product-16
仓库中还没有被消费的产品数量:2
Producer[producer-2]storageProduct:Product-17
仓库中还没有被消费的产品数量:3
Producer[producer-3]storageProduct:Product-18
仓库中还没有被消费的产品数量:4
Consumer[consumer-3] getProduct: Product-15
仓库中还没有被消费的产品数量:3