生产-消费者的两种实现方式

第一种:
Storage:

public class Storage {
    private final int MAX_SIZE = 100;
    private LinkedList<Object> list = new LinkedList<>();
    public void produce(int num){
        synchronized (Storage.class){
            for(int i=0;i<num;i++){
                if(list.size()+1>MAX_SIZE){
                    System.out.println("此时库存量为:"+list.size()+"已达到最大容量!不能进行生产操作");
                    try
                    {
                        Storage.class.wait();
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }else{
                    list.add(new Object());
                    System.out.println("已经生产了1个产品,现在库存量为"+list.size());
                }
            }
            Storage.class.notifyAll();
        }
    }
    public void consume(int num){
        synchronized (Storage.class){
            for(int i=0;i<num;i++){
                if(list.size()==0){
                    System.out.println("此时库存量为0,不能进行消费操作");
                    try
                    {
                        Storage.class.wait();
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }else{
                    list.remove();
                    System.out.println("已经消费了1个产品,现在库存量为"+list.size());
                }
            }
            Storage.class.notifyAll();
        }
    }

    public int getMAX_SIZE() {
        return MAX_SIZE;
    }

    public LinkedList<Object> getList() {
        return list;
    }

    public void setList(LinkedList<Object> list) {
        this.list = list;
    }
}

Producer:

public class Producer extends Thread
{
    // 每次生产的产品数量
    private int num;

    // 所在放置的仓库
    private Storage storage;

    // 构造函数,设置仓库
    public Producer(Storage storage)
    {
        this.storage = storage;
    }

    // 线程run函数
    public void run()
    {
        produce(num);
    }

    // 调用仓库Storage的生产函数
    public void produce(int num)
    {
        storage.produce(num);
    }

    // get/set方法
    public int getNum()
    {
        return num;
    }

    public void setNum(int num)
    {
        this.num = num;
    }

    public Storage getStorage()
    {
        return storage;
    }

    public void setStorage(Storage storage)
    {
        this.storage = storage;
    }
}

Consumer:

public class Consumer extends Thread
{
    // 每次消费的产品数量
    private int num;

    // 所在放置的仓库
    private Storage storage;

    // 构造函数,设置仓库
    public Consumer(Storage storage)
    {
        this.storage = storage;
    }

    // 线程run函数
    public void run()
    {
        consume(num);
    }

    // 调用仓库Storage的生产函数
    public void consume(int num)
    {
        storage.consume(num);
    }

    // get/set方法
    public int getNum()
    {
        return num;
    }

    public void setNum(int num)
    {
        this.num = num;
    }

    public Storage getStorage()
    {
        return storage;
    }

    public void setStorage(Storage storage)
    {
        this.storage = storage;
    }

Test:

public class Test
{
    public static void main(String[] args)
    {
        // 仓库对象
        Storage storage = new Storage();

        // 生产者对象
        Producer p1 = new Producer(storage);
        Producer p2 = new Producer(storage);
        Producer p3 = new Producer(storage);
        Producer p4 = new Producer(storage);

        // 消费者对象
        Consumer c1 = new Consumer(storage);
        Consumer c2 = new Consumer(storage);
        Consumer c3 = new Consumer(storage);

        // 设置生产者产品生产数量
        p1.setNum(10);
        p2.setNum(10);
        p3.setNum(10);
        p4.setNum(10);


        // 设置消费者产品消费数量
        c1.setNum(50);
        c2.setNum(20);
        c3.setNum(30);

        // 线程开始执行
        c1.start();
        c2.start();
        c3.start();
        p1.start();
        p2.start();
        p3.start();
        p4.start();

    }
}

测试结果:

Thread[Thread-4,5,main]此时库存量为0,不能进行消费操作
Thread[Thread-2,5,main]已经生产了1个产品,现在库存量为1
Thread[Thread-2,5,main]已经生产了1个产品,现在库存量为2
Thread[Thread-2,5,main]已经生产了1个产品,现在库存量为3
Thread[Thread-2,5,main]已经生产了1个产品,现在库存量为4
Thread[Thread-2,5,main]已经生产了1个产品,现在库存量为5
Thread[Thread-2,5,main]已经生产了1个产品,现在库存量为6
Thread[Thread-2,5,main]已经生产了1个产品,现在库存量为7
Thread[Thread-2,5,main]已经生产了1个产品,现在库存量为8
Thread[Thread-2,5,main]已经生产了1个产品,现在库存量为9
Thread[Thread-2,5,main]已经生产了1个产品,现在库存量为10
Thread[Thread-0,5,main]已经生产了1个产品,现在库存量为11
Thread[Thread-0,5,main]已经生产了1个产品,现在库存量为12
Thread[Thread-0,5,main]已经生产了1个产品,现在库存量为13
Thread[Thread-0,5,main]已经生产了1个产品,现在库存量为14
Thread[Thread-0,5,main]已经生产了1个产品,现在库存量为15
Thread[Thread-0,5,main]已经生产了1个产品,现在库存量为16
Thread[Thread-0,5,main]已经生产了1个产品,现在库存量为17
Thread[Thread-0,5,main]已经生产了1个产品,现在库存量为18
Thread[Thread-0,5,main]已经生产了1个产品,现在库存量为19
Thread[Thread-0,5,main]已经生产了1个产品,现在库存量为20
Thread[Thread-3,5,main]已经生产了1个产品,现在库存量为21
Thread[Thread-3,5,main]已经生产了1个产品,现在库存量为22
Thread[Thread-3,5,main]已经生产了1个产品,现在库存量为23
Thread[Thread-3,5,main]已经生产了1个产品,现在库存量为24
Thread[Thread-3,5,main]已经生产了1个产品,现在库存量为25
Thread[Thread-3,5,main]已经生产了1个产品,现在库存量为26
Thread[Thread-3,5,main]已经生产了1个产品,现在库存量为27
Thread[Thread-3,5,main]已经生产了1个产品,现在库存量为28
Thread[Thread-3,5,main]已经生产了1个产品,现在库存量为29
Thread[Thread-3,5,main]已经生产了1个产品,现在库存量为30
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为29
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为28
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为27
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为26
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为25
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为24
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为23
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为22
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为21
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为20
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为19
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为18
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为17
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为16
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为15
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为14
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为13
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为12
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为11
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为10
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为9
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为8
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为7
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为6
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为5
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为4
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为3
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为2
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为1
Thread[Thread-6,5,main]已经消费了1个产品,现在库存量为0
Thread[Thread-5,5,main]此时库存量为0,不能进行消费操作
Thread[Thread-4,5,main]此时库存量为0,不能进行消费操作
Thread[Thread-1,5,main]已经生产了1个产品,现在库存量为1
Thread[Thread-1,5,main]已经生产了1个产品,现在库存量为2
Thread[Thread-1,5,main]已经生产了1个产品,现在库存量为3
Thread[Thread-1,5,main]已经生产了1个产品,现在库存量为4
Thread[Thread-1,5,main]已经生产了1个产品,现在库存量为5
Thread[Thread-1,5,main]已经生产了1个产品,现在库存量为6
Thread[Thread-1,5,main]已经生产了1个产品,现在库存量为7
Thread[Thread-1,5,main]已经生产了1个产品,现在库存量为8
Thread[Thread-1,5,main]已经生产了1个产品,现在库存量为9
Thread[Thread-1,5,main]已经生产了1个产品,现在库存量为10
Thread[Thread-4,5,main]已经消费了1个产品,现在库存量为9
Thread[Thread-4,5,main]已经消费了1个产品,现在库存量为8
Thread[Thread-4,5,main]已经消费了1个产品,现在库存量为7
Thread[Thread-4,5,main]已经消费了1个产品,现在库存量为6
Thread[Thread-4,5,main]已经消费了1个产品,现在库存量为5
Thread[Thread-4,5,main]已经消费了1个产品,现在库存量为4
Thread[Thread-4,5,main]已经消费了1个产品,现在库存量为3
Thread[Thread-4,5,main]已经消费了1个产品,现在库存量为2
Thread[Thread-4,5,main]已经消费了1个产品,现在库存量为1
Thread[Thread-4,5,main]已经消费了1个产品,现在库存量为0
Thread[Thread-4,5,main]此时库存量为0,不能进行消费操作
Thread[Thread-5,5,main]此时库存量为0,不能进行消费操作

第二种(LinkedBlockingQueue )

只修改Storage其他不变

public class Storage {
    private final int MAX_SIZE = 100;
    private LinkedBlockingQueue<Object> list = new LinkedBlockingQueue<>(100);
    public void produce(int num){
           if(list.size()==MAX_SIZE){
               System.out.println(Thread.currentThread ()+"此时库存量:"+MAX_SIZE+",容量已满,不能进行生产操作");
           }
           for(int i=0;i<num;i++){
               try{
                   list.put(new Object());

               }catch (InterruptedException e){
                   e.printStackTrace();
               }
               System.out.println(Thread.currentThread ()+"已经生产了1个商品,此时容量为:"+list.size());
           }
    }
    public void consume(int num){
            if(list.size()==0){
                System.out.println(Thread.currentThread ()+"此时库存量:0,不能进行消费操作");
            }
            for(int i=0;i<num;i++){
                try{
                    list.take();

                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread ()+"已经消费了1个商品,此时容量为:"+list.size());
            }
    }

    public int getMAX_SIZE() {
        return MAX_SIZE;
    }

    public LinkedBlockingQueue<Object> getList() {
        return list;
    }

    public void setList(LinkedBlockingQueue<Object> list) {
        this.list = list;
    }
}

ps:用LinkedBlockingQueue最后结果打印的时候可能和想象的不一致,是因为如果此时容量已满,在执行put操作时候,内部会调用await()方法,释放了线程的执行权,然后消费者线程拿到执行权调用take()方法,当消费完毕后通知生产者时候会调用signal()方法, 但是这个时候消费者或者还没有打印完我们定义的语句,然后生产者生产又开始打印生产语句,导致输出语句不匹配,所以这个是正常的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值