我的漫漫程序之旅

          專注于JavaWeb開發
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數據加載中……

          生產者消費者問題(以面包為例)

          package Thread;

          public class ProducerConsumer 
          {
              
          public static void main(String[] args) 
              
          {
                  SynchronizedStack ss 
          = new SynchronizedStack();
                  Producer p 
          = new Producer(ss); //產生一個生產者
                  Consumer c = new Consumer(ss); //產生一個消費者
                  new Thread(p).start(); //啟動生產者線程
                  new Thread(c).start(); //啟動消費者線程
                  
              }


          }


          class Bread //定義生產面包
          {
              
          int id;
              Bread(
          int id)
              
          {
                  
          this.id = id;
              }

              
              
          public String toString() //重寫toString方法
              {
                  
          return "bread :" + id;
              }

          }


          class SynchronizedStack //定義一個盛面包的容器
          {
              
          int index = 0;
              Bread[] bread 
          = new Bread[6];
              
              
          public synchronized void putIn(Bread bread) // 放進方法
              {
                  
          while(index == this.bread.length)
                  
          {
                      
          try 
                      
          {
                          
          this.wait(); // 只針對synchronized
          //Object對象的wait()方法,表示調用當前對象的wait()方法,運行當前對象的此方法的線程等待,直到被notify()喚醒
                      }
           
                      
          catch (InterruptedException e) 
                      
          {
                          e.printStackTrace();
                      }

                  }

                  
          this.notify(); //喚醒一個wait的線程
          //        this.notifyAll();//喚醒所有wait的線程
                  this.bread[index] = bread;
                  index 
          ++;
              }

              
              
          public synchronized Bread putOut() // 拿出方法
              {
                  
          while(index == 0)
                  
          {
                      
          try 
                      
          {
                          
          this.wait();
                      }
           
                      
          catch (InterruptedException e) 
                      
          {
                          e.printStackTrace();
                      }

                  }

                  
          this.notify(); //喚醒一個wait的線程
          //        this.notifyAll();//喚醒所有wait的線程
                  index --;
                  
          return bread[index];
              }

          }


          class Producer implements Runnable
          {
              SynchronizedStack ss 
          = null;
              Producer(SynchronizedStack ss)
              
          {
                  
          this.ss = ss;
              }

              
          public void run() 
              
          {
                  
          for(int i=0;i<30;i++)
                  
          {
                      Bread bread 
          = new Bread(i);
                      ss.putIn(bread);
                      System.out.println(
          "生產了 :" + bread);
                      
          try
                      
          {
          //                Thread.sleep(1000);
                          Thread.sleep((int)Math.random() * 1000);
                      }

                      
          catch(InterruptedException e)
                      
          {
                          e.printStackTrace();
                      }

                  }

              }

          }


          class Consumer implements Runnable
          {
              SynchronizedStack ss 
          = null;
              Consumer(SynchronizedStack ss)
              
          {
                  
          this.ss = ss;
              }

              
          public void run() 
              
          {
                  
          for(int i=0;i<30;i++)
                  
          {
                      Bread bread 
          = ss.putOut();
                      System.out.println(
          "消費了 :" + bread);
                      
          try
                      
          {
          //                Thread.sleep(1000);
                          Thread.sleep((int)Math.random() * 1000);
                      }

                      
          catch(InterruptedException e)
                      
          {
                          e.printStackTrace();
                      }

                  }

              }

          }


          posted on 2007-12-10 22:41 々上善若水々 閱讀(788) 評論(0)  編輯  收藏 所屬分類: J2SE

          主站蜘蛛池模板: 和硕县| 广德县| 米林县| 阿拉善右旗| 蓝田县| 银川市| 苏州市| 富川| 金湖县| 望江县| 新余市| 资兴市| 东平县| 巨野县| 集安市| 辽宁省| 沧州市| 南华县| 内江市| 宝应县| 桦甸市| 建水县| 鄱阳县| 灵武市| 民权县| 顺义区| 商河县| 墨玉县| 南通市| 汉寿县| 乐东| 浮山县| 确山县| 志丹县| 延庆县| 乡宁县| 东城区| 冀州市| 徐水县| 调兵山市| 赤壁市|