请问老师在wait手写生产者消费者的案例的时候,如果我弄了2个生产者和1个消费者,程序为什么不会停止,为什么最后还是会生产

来源:7-6 用wait/notify实现

慕瓜4980759

2019-09-25

package com.johnny.threadobjectcommonsmethods;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class WaitNotifyProducerConsumer {

public static void main(String[] args) {

    Storage storage = new Storage();

    Producer producer = new Producer(storage);
    Consume consume = new Consume(storage);


    Thread producerThread = new Thread(producer);
    Thread producerThread2 = new Thread(producer);
    Thread consumeThread = new Thread(consume);

    producerThread.setName("ProducerThread");
    //producerThread2.setName("ProducerThread2");
    consumeThread.setName("ConsumerThread");

    producerThread.start();
    //producerThread2.start();
    consumeThread.start();


}


static class Producer implements Runnable {

    private Storage storage;

    private int count;

    public Producer(Storage storage) {
        this.storage = storage;
    }

    public void run() {
        for (int i = 0; i < 100; i++) {
            count++;
            storage.put();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("生产了" + count);
    }
}

static class Consume implements Runnable {

    private Storage storage;

    private int count;

    public Consume(Storage storage) {
        this.storage = storage;
    }

    public void run() {
        for (int i = 0; i < 100; i++) {
            count++;
            storage.take();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("消费了" + count);
    }
}


static class Storage {

    //这个容器最大的容量
    private int maxSize;

    private LinkedList<Date> list;

    public Storage() {
        maxSize = 10;
        list = new LinkedList<Date>();
    }

    public synchronized void put() {

        //如果队列已经满了 就让当前生产者线程 进入wait
        while (list.size() == maxSize) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        list.add(new Date());
        System.out.println(Thread.currentThread().getName() + " 生产者 生产了 当前队列已有 " + list.size());
        //生产了一个 立即通知
        notify();
    }

    public synchronized void take() {
        while (list.size() == 0) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //poll 方法 取出第一个元素,类似于出栈
        System.out.println(Thread.currentThread().getName() + " 消费者 消费了  " + list.poll() + "当前队列还有" + list.size());
        notify();
    }
}

}

写回答

1回答

慕瓜4980759

提问者

2019-09-25

本人真的是菜到抠脚 啊  我把代码改成这样 就行了 能够保证多个生产者总共只会生产100个

for (int i = 0; i < 100; i++) {
   synchronized (resourceLock){
       if(count == 100){
           break;
       }
       count++;
       storage.put();
   }
}

0
1
悟空
恩好的
2019-09-25
共1条回复

线程八大核心+Java并发原理及企业级并发解决方案

完整的并发知识网络+丰富的工作内容分享+50余道并发高频面试题

2512 学习 · 939 问题

查看课程