var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-20738293-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script')"/>

          jutleo
          歡迎走進有風的地方~~
          posts - 63,  comments - 279,  trackbacks - 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 19:48 凌晨風 閱讀(2520) 評論(4)  編輯  收藏 所屬分類: Java學習筆記

          FeedBack:
          # re: 生產者消費者問題(以面包為例)
          2007-12-10 21:35 | wǒ愛伱--咾婆
          呵呵..支持了...好東東...寫的不錯啊..  回復  更多評論
            
          # re: 生產者消費者問題(以面包為例)
          2007-12-11 00:28 | zsulzm
          這樣貌似有問題吧
            回復  更多評論
            
          # re: 生產者消費者問題(以面包為例)
          2008-04-02 15:15 | 不行吧
          吃面包的時候不能往里面放面包的。  回復  更多評論
            
          # re: 生產者消費者問題(以面包為例)[未登錄]
          2008-04-28 15:29 | z
          有點問題,輸出固定不夠明顯.Consumer和Producer中不必都有 try
          {
          // Thread.sleep(1000);
          Thread.sleep((int)Math.random() * 1000);
          }
          catch(InterruptedException e)
          {
          e.printStackTrace();
          }
          去掉一個,就明顯多了.  回復  更多評論
            

          <2007年12月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿(11)

          我參與的團隊

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          新聞分類

          新聞檔案

          收藏夾

          圍脖

          最新隨筆

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 壶关县| 平遥县| 天长市| 天津市| 渝中区| 阿拉善右旗| 五河县| 三亚市| 宁南县| 从化市| 正阳县| 白河县| 桃源县| 辉县市| 永春县| 印江| 朝阳区| 和硕县| 兴安县| 丰镇市| 琼结县| 杨浦区| 伊宁县| 淄博市| 乌鲁木齐市| 仁怀市| 航空| 长乐市| 咸宁市| 大化| 庄河市| 金寨县| 峡江县| 云安县| 万宁市| 信阳市| 乳山市| 蚌埠市| 闵行区| 山东| 岗巴县|