老师,我实现多个生产者和消费者,线程没有停止运行,您能帮我看看不

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

manong22

2019-10-14


package src.threadcoreknowledge.threadobjectclasscommonmethods;

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

/**
* @program: muti_thread_AND_hign_concurrency
* @author: yaopeng
* @create: 2019-10-14 09:06
**/
public class ProducerConsumer {

   public static void main(String[] args) {
       LinkedList<Date> LinkedList = new LinkedList<>();

       EventStorage storage = new EventStorage();

       Producer producer  = new Producer(storage);
       Consumer consumer  = new Consumer(storage);

       Thread thread1 = new Thread(producer);
       Thread thread2 = new Thread(producer);
       Thread thread3 = new Thread(consumer);
       Thread thread4 = new Thread(consumer);
       thread1.setName("生产者1");
       thread2.setName("生产者2");
       thread3.setName("消费者1");
       thread4.setName("消费者2");
       thread1.start();
       thread2.start();
       thread3.start();
       thread4.start();

   }
}


   class Producer implements  Runnable{
       private EventStorage storage;
       private  Integer account ;

       public Producer(EventStorage storage) {
           this.storage = storage;
           this.account = 0;
       }

       @Override
       public void run() {
           for(int i = 0; i<100;i++){
               synchronized (this){
                   if(account == 100){
                       break;
                   }else{
                   account++;
                   storage.put();
                   System.out.println(account);
                   }
               }
           }
       }
   }


   class Consumer implements  Runnable{
       private EventStorage storage;

       public Consumer(EventStorage storage) {
           this.storage = storage;
       }

       @Override
       public void run() {
           for(int i = 0; i<100;i++){
               storage.take();
           }
       }
   }


   class EventStorage{
       private Integer max_size;
       private LinkedList<Date> linkedList;

       public EventStorage() {
           this.max_size = 10;
           this.linkedList = new LinkedList<>();
       }

       public synchronized void put(){
           while(linkedList.size()>= max_size){
               try {
                   wait();
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
           linkedList.add(new Date());
           System.out.println(Thread.currentThread().getName()+"生产了一个产品,目前有"+linkedList.size()+"个产品");
           notifyAll();

       }


       public synchronized void take(){
           while(linkedList.size()<=0){
               try {
                   wait();
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }

           linkedList.poll();
           System.out.println(Thread.currentThread().getName()+"消费了一个产品,还剩"+linkedList.size()+"个产品");
           notifyAll();
       }
   }





写回答

2回答

chenlianlian

2019-11-03

你有两个生产者线程两个消费者线程,出现问题的原因在于是你的生产者线程已经结束了,仓库里也已经被消费完了,那么如果此时有消费者线程处于waiting状态时,你又没有去唤醒它,它就不会终止

0
0

悟空

2019-10-14

可能是put和take期间进入wait了,没人唤醒,所以不停止。你可以把总数量改成3,然后打断点调试一下,很快可以发现问题。

0
0

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

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

2512 学习 · 939 问题

查看课程