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

          生產者-消費者問題,是操作系統的進程管理中很重要的問題。以前的我都是用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狂 閱讀(2058) 評論(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)

          常去逛逛

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 互助| 上饶县| 客服| 大同市| 阿巴嘎旗| 鹿邑县| 微山县| 越西县| 南宫市| 宁津县| 宜川县| 谷城县| 巩义市| 临海市| 永修县| 铜陵市| 翁源县| 高安市| 安丘市| 徐汇区| 天镇县| 纳雍县| 黄骅市| 汨罗市| 广灵县| 保靖县| 托克托县| 岑巩县| 孝义市| 始兴县| 望都县| 漳州市| 陕西省| 司法| 商河县| 吉首市| 蕉岭县| 白玉县| 临洮县| 麟游县| 阿图什市|