一種實現生產者-消費者問題的新方法

          生產者-消費者問題,是操作系統的進程管理中很重要的問題。以前的我都是用wait(),notify()方法來實現的。今天我在翻閱JDK1.5API幫助文檔的時候,偶然間發現了一個類ArrayBlockingQueue,它其中提供了以下兩個方法:

          void put (E o)
                    Adds the specified element to the tail of this queue, waiting if necessary for space to become available.
           E take()
                    Retrieves and removes the head of this queue, waiting if no elements are present on this queue.


          我靈機一動,用這兩個JDK中定義好的方法是不是也可以實現生產者-消費者問題呢?試了一下,嘿,還真靈。

          /*
           *@author 我為J狂 建立日期 2007-3-25
           *
           
          */

          package net.blogjava.lzqdiy;

          import java.util.concurrent.ArrayBlockingQueue;

          public class TestUtil
          {
              
          private ArrayBlockingQueue<String> queue ;

              
          /**
               * 
          @param args
               
          */

              
          public TestUtil(int capacity)
              
          {
                  queue 
          = new ArrayBlockingQueue<String>(capacity);
              }

              
          class Consumer extends Thread
              
          {

                  @Override
                  
          public void run()
                  
          {
                      
          // TODO Auto-generated method stub
                      while (true)
                      
          {
                          
          synchronized (queue)
                          
          {
                              
          try
                              
          {
                                  System.out.println(
          "take:" + queue.take());
                                  Thread.sleep(
          5);
                              }
           catch (InterruptedException e)
                              
          {
                                  
          // TODO Auto-generated catch block
                                  e.printStackTrace();
                              }

                          }

                      }

                  }

              }


              
          class Producer extends Thread
              
          {

                  
          private int i;
                  @Override
                  
          public void run()
                  
          {
                      
          // TODO Auto-generated method stub
                      while (true)
                      
          {
                          
          synchronized (queue)
                          
          {
                              
          try
                              
          {
                                  queue.put((
          ++i) + "");
                                  System.out.println(
          "put:" + i);
                                  Thread.sleep(
          1);
                              }
           catch (InterruptedException e)
                              
          {
                                  
          // TODO Auto-generated catch block
                                  e.printStackTrace();
                              }

                          }

                      }

                  }

              }


              
          public static void main(String[] args)
              
          {
                  TestUtil t 
          = new TestUtil(10);//阻塞隊列的容量為10
                  TestUtil.Producer p = t.new Producer();
                  p.start();
                  
                  TestUtil.Consumer c 
          = t.new Consumer();
                  c.start();
              }

          }

          這個算法有點小BUG,就是當隊列容量過小時,有可能發生死鎖。希望大家留言,來改進這個算法。

          posted on 2007-03-25 14:17 我為J狂 閱讀(2055) 評論(3)  編輯  收藏 所屬分類: 線程

          評論

          # re: 一種實現生產者-消費者問題的新方法。 2007-03-25 15:05 dennis

          jdk5引入的concurrent包,了解太少,這方面的學習資料也少  回復  更多評論   

          # re: 一種實現生產者-消費者問題的新方法 2007-05-08 22:04 www.bushiba.com

          不懂哦,只知道老師要寫偶寫出來  回復  更多評論   

          # re: 一種實現生產者-消費者問題的新方法 2008-08-07 11:07 pei

          用了BlockingQueue還要synchronized嗎?  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2007年3月>
          25262728123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統計

          常用鏈接

          留言簿(11)

          隨筆分類(48)

          文章分類(29)

          常去逛逛

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 平凉市| 长武县| 麦盖提县| 灵寿县| 奉化市| 尉氏县| 新巴尔虎左旗| 广平县| 含山县| 天台县| 灵丘县| 乐平市| 涞水县| 宜君县| 汶上县| 九台市| 东乌| 会同县| 缙云县| 特克斯县| 淮南市| 河源市| 曲水县| 泽州县| 和龙市| 紫金县| 屯留县| 容城县| 虞城县| 青神县| 岑溪市| 荣昌县| 江陵县| 电白县| 平乐县| 光山县| 兰溪市| 如皋市| 铁力市| 玛纳斯县| 合川市|