我的漫漫程序之旅

          專注于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

          主站蜘蛛池模板: 县级市| 开平市| 郯城县| 桑植县| 光泽县| 榆树市| 象州县| 嘉定区| 郁南县| 民和| 福建省| 荔浦县| 兴文县| 读书| 阿荣旗| 荥阳市| 嘉荫县| 伊金霍洛旗| 松滋市| 方城县| 惠安县| 虎林市| 铜山县| 寻甸| 普兰县| 青州市| 车致| 泰安市| 城市| 湛江市| 馆陶县| 郴州市| 固原市| 明溪县| 广州市| 营口市| 黑水县| 福泉市| 丹凤县| 中西区| 五台县|