一種實(shí)現(xiàn)生產(chǎn)者-消費(fèi)者問題的新方法

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

          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.


          我靈機(jī)一動(dòng),用這兩個(gè)JDK中定義好的方法是不是也可以實(shí)現(xiàn)生產(chǎn)者-消費(fèi)者問題呢?試了一下,嘿,還真靈。

          /*
           *@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);//阻塞隊(duì)列的容量為10
                  TestUtil.Producer p = t.new Producer();
                  p.start();
                  
                  TestUtil.Consumer c 
          = t.new Consumer();
                  c.start();
              }

          }

          這個(gè)算法有點(diǎn)小BUG,就是當(dāng)隊(duì)列容量過小時(shí),有可能發(fā)生死鎖。希望大家留言,來改進(jìn)這個(gè)算法。

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

          評(píng)論

          # re: 一種實(shí)現(xiàn)生產(chǎn)者-消費(fèi)者問題的新方法。 2007-03-25 15:05 dennis

          jdk5引入的concurrent包,了解太少,這方面的學(xué)習(xí)資料也少  回復(fù)  更多評(píng)論   

          # re: 一種實(shí)現(xiàn)生產(chǎn)者-消費(fèi)者問題的新方法 2007-05-08 22:04 www.bushiba.com

          不懂哦,只知道老師要寫偶寫出來  回復(fù)  更多評(píng)論   

          # re: 一種實(shí)現(xiàn)生產(chǎn)者-消費(fèi)者問題的新方法 2008-08-07 11:07 pei

          用了BlockingQueue還要synchronized嗎?  回復(fù)  更多評(píng)論   


          只有注冊用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2007年3月>
          25262728123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(11)

          隨筆分類(48)

          文章分類(29)

          常去逛逛

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 梁山县| 庆元县| 溧阳市| 沐川县| 凭祥市| 咸阳市| 抚州市| 中超| 石河子市| 明水县| 桦南县| 太仓市| 沙河市| 荆州市| 禹州市| 崇礼县| 黄平县| 八宿县| 崇仁县| 仁布县| 兴化市| 南木林县| 蒲城县| 烟台市| 顺义区| 天津市| 方正县| 开阳县| 体育| 台湾省| 嘉定区| 启东市| 虎林市| 菏泽市| 昭平县| 衡阳县| 陕西省| 佛山市| 郧西县| 乾安县| 宁晋县|