自己用Java写的简单生产者与消费者模型

生产者:

package com.mary.product;

public class Producer implements Runnable {
    private String name;
    private Storage store;

    public Producer(String name, Storage store) {
        super();
        this.name = name;
        this.store = store;
    }

    @Override
    public void run() {
        while (true) {
            try {
                synchronized (store) {
                    if (store.isFull()) {
                        System.out.println("【" + name + "】发现仓库满了,等待消费者消费");
                        store.wait();
                    } else {
                        Product p = new Product("产品" + (int) (Math.random() * 1000), (int) (Math
                                .random() * 1000));
                        store.push(p);
                        System.out.println("【" + name + "】生产了一个产品【" + p.toString() + "】库存量:"
                                + store.getList().size());
                        store.notifyAll();
                    }
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {

                e.printStackTrace();
            }
        }
    }
}

消费者:

package com.mary.product;

public class Consumer implements Runnable {
    private String name;
    private int num;
    private Storage store;

    public Consumer(String name, int num, Storage store) {
        super();
        this.name = name;
        this.num = num;
        this.store = store;
    }

    @Override
    public void run() {
        while (true) {
            try {
                synchronized (store) {
                    if (store.isEmpty() || store.size() < num) {
                        System.out.println("【" + name + "】需要[" + num + "]发现库存量不足,等待生产者生产");
                        store.wait();
                    } else {
                        for (int i = 0; i < num; i++) {
                            store.pop();
                        }
                        System.out.println("【" + name + "】消费了" + num + "个产品"
                                + store.getList().size());
                        store.notifyAll();
                    }
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


产品:

package com.mary.product;

public class Product {
    private String name;
    private int price;

    public Product() {
        super();
    }

    public Product(String name, int price) {
        super();
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public String toString() {

        return "产品名称:" + name + ",价格: " + price;
    }

}


仓库:

package com.mary.product;

import java.util.ArrayList;
import java.util.List;

public class Storage {
    private List<Product> list;
    public final int max = 10;

    public Storage() {
        super();
        list = new ArrayList<Product>();
    }

    public synchronized void push(Product p) {
        list.add(p);
    }

    public synchronized void pop() {
        list.remove(0);
    }

    public synchronized boolean isEmpty() {
        return list.size() == 0 ? true : false;
    }

    public synchronized boolean isFull() {
        return list.size() == max ? true : false;
    }

    public synchronized List<Product> getList() {
        return list;
    }

    public int size() {
        return list.size();
    }
}

测试:

package com.mary.product;

public class Market {
    public static void main(String[] args) {
        Storage store = new Storage();
        Producer p1 = new Producer("生产者1号", store);
        Producer p2 = new Producer("生产者2号", store);
        Producer p3 = new Producer("生产者3号", store);
        Consumer c1 = new Consumer("消费者1号", 1, store);
        Consumer c2 = new Consumer("消费者2号", 2, store);
        Consumer c3 = new Consumer("消费者3号", 3, store);
        new Thread(p1).start();
        new Thread(p2).start();
        new Thread(p3).start();
        new Thread(c1).start();
        new Thread(c2).start();
        new Thread(c3).start();
    }
}

运行结果:




源码下载地址:http://download.csdn.net/detail/yeruowei/6878003


如果有异议,欢迎各位批评指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值