zoukankan      html  css  js  c++  java
  • wait和notify实现的生产者消费者线程交互

    public class ProductTest
     {
          public static void main(String args[])
          {
               Repertory repertory=new Repertory();
              new Thread(new Producer(repertory)).start();
              new Thread(new Consumer(repertory)).start();
          }
    }
    
    
    class Repertory{
          private int product=0;
    
          public synchronized void addProduct()
           {
                   if(this.product>=5)
                      {
                          try{
                                 this.wait();
                              }catch(Exception e)
                             {
                             e.printStackTrace();
                              }
                      }
                 else
                   {
                   product++;
                   System.out.println("生产者生成第"+product+"个产品");
                   this.notifyAll();
                 }
         }
    
          public synchronized void getProduct()
      {
              if(this.product<=0)
              {
                        try{
                              this.wait();
                            }catch(Exception e)
                                {
                                  e.printStackTrace();
                                 }
              }
             else
               {
                   System.out.println("消费者取走了第"+product+"个产品");
                   product--;
                  this.notifyAll();
              }
           }
    
      }
    
    
    class Producer implements Runnable{
        private Repertory repertory;
        public Producer(Repertory repertory)
       {
        this.repertory=repertory;
        }
    
        public void run(){
           System.out.println("生产者開始生产产品");
             while(true)
          {
              try{
                 Thread.sleep((int)(Math.random()*10)*100);
                  }catch(Exception e)
                 {
                    e.printStackTrace();
                 }
                repertory.addProduct();
            }
       }
    }
    
    
    
    
    class Consumer implements Runnable{
         private Repertory repertory;
         public Consumer(Repertory repertory)
        {
         this.repertory=repertory;
        }
    
          public void run()
          {
             System.out.println("消费者開始取走产品");
             while(true)
            {
                    try{
                         Thread.sleep((int)(Math.random()*10)*100);
                       }catch(Exception e)
                      {
                            e.printStackTrace();
                     }
                    repertory.getProduct();
           }
         }
    }


  • 相关阅读:
    Linux命令比较文件内容
    Linux命令jobs小记
    权限控制框架
    Log4j2源码分析系列:(一)配置加载
    Callable和Supplier的区别
    排序算法之归并排序
    Spring boot整合Mybatis
    排序算法之堆排序
    排序算法之希尔排序
    排序算法之快速排序
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/7249461.html
Copyright © 2011-2022 走看看