<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-348  評論-598  文章-0  trackbacks-0

    之前我們已經用常用方法寫了一個消費者與生產者程序,不過看上去有點煩。在JDK 5里面,Java為我們提供了一個可以簡化這方面開發的的接口

    java.util.concurrent.BlockingQueue
    使用BlockingQueue,我們的程序可以這樣寫
    import java.util.concurrent.BlockingQueue;

    public class ConsumerBlockingQueue extends Thread {

        
    private final BlockingQueue<Integer> queue;
        
    private final String name;
        
        
    public ConsumerBlockingQueue(BlockingQueue<Integer> q, String name)
        
    {
            queue
    =q;
            
    this.name=name;
        }

        
    public void run() {
            
    // TODO Auto-generated method stub
            try
            
    {
                
    while(true)
                
    {
                    consume(queue.take());
                    
    try
                    
    {
                        sleep(
    800);// 將消費者的睡眠時間設置得比生產者小是為了演示當產品列表為空的情形
                    }
    catch(Exception e){
                        e.printStackTrace();
                    }

                }

            }
    catch(Exception e){
                e.printStackTrace();
            }

        }

        
        
    private void consume(int i)
        
    {
            System.out.println(name
    +" consume "+i);
        }


    }

    這個是消費者類。
    public class ProducerBlockingQueue extends Thread{
        
        
    private final BlockingQueue<Integer> queue;
        
    private final String name;
        
    private static int i=0;
        
    public ProducerBlockingQueue(BlockingQueue<Integer> q, String name)
        
    {
            queue
    =q;
            
    this.name=name;
        }

        
        
    public void run() {
            
    // TODO Auto-generated method stub
            try
            
    {
                
    while(true)
                
    {
                    queue.add(produce());
                    
    try
                    
    {
                        sleep(
    1000);
                    }
    catch(Exception e){
                        e.printStackTrace();
                    }

                }

                    
            }
    catch(Exception e){
                e.printStackTrace();
            }


        }

        
        
    private int produce()
        
    {
            System.out.println(name
    +" producing "+i);
            
    return i++;
        }


    }

    這個是生產者類。
    import java.util.*;
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.TimeUnit;

    public class Queue implements BlockingQueue {

        
    private List list=new ArrayList();
        
    public boolean add(Object o) {
            
    // TODO Auto-generated method stub
            list.add(o);
            
    return true;
        }


        
    public Object take() throws InterruptedException {
            
    // TODO Auto-generated method stub
            while(isEmpty()){}
            
    return list.remove(0);
        }



        
    public boolean isEmpty() {
            
    // TODO Auto-generated method stub
            return list.isEmpty();
        }

    // 當然這個類還有其他的方法需要實現,為了清楚起見,我把使用默認實現的方法都去掉了。

    }

    我們定義一個Queue來實現BlockingQueue。下面我們來測試下
    import java.util.concurrent.BlockingQueue;


    public class Test {

        
    /**
         * 
    @param args
         
    */

        
    public static void main(String[] args) {
            
    // TODO Auto-generated method stub
            BlockingQueue<Integer> q=new Queue();
            ProducerBlockingQueue p
    =new ProducerBlockingQueue(q,"p");
            ProducerBlockingQueue p1
    =new ProducerBlockingQueue(q,"p1");
            ConsumerBlockingQueue c
    =new ConsumerBlockingQueue(q,"c");
            ConsumerBlockingQueue c1
    =new ConsumerBlockingQueue(q,"c1");
            p.start();
            p1.start();
            c.start();
            c1.start();
        }


    }

    看到沒有,就這么簡單,以很少的邏輯代碼實現了消費者與生產者功能,當然你還可以對他進行擴充,讓他更加完善。


    ---------------------------------------------------------
    專注移動開發

    Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
    posted on 2007-04-28 12:11 TiGERTiAN 閱讀(2411) 評論(4)  編輯  收藏 所屬分類: Java

    評論:
    # re: 使用BlockingQueue來簡化消費者與生產者的問題 2007-04-28 14:18 | 王凌華
    Java文檔里面也有個類似的玩意,

    -----------------------------------------------------------------
    class Producer implements Runnable {
    private final BlockingQueue queue;
    Producer(BlockingQueue q) { queue = q; }
    public void run() {
    try {
    while (true) { queue.put(produce()); }
    } catch (InterruptedException ex) { ... handle ...}
    }
    Object produce() { ... }
    }

    class Consumer implements Runnable {
    private final BlockingQueue queue;
    Consumer(BlockingQueue q) { queue = q; }
    public void run() {
    try {
    while (true) { consume(queue.take()); }
    } catch (InterruptedException ex) { ... handle ...}
    }
    void consume(Object x) { ... }
    }

    class Setup {
    void main() {
    BlockingQueue q = new SomeQueueImplementation();
    Producer p = new Producer(q);
    Consumer c1 = new Consumer(q);
    Consumer c2 = new Consumer(q);
    new Thread(p).start();
    new Thread(c1).start();
    new Thread(c2).start();
    }
    }

    你不是抄襲的吧。:)   回復  更多評論
      
    # re: 使用BlockingQueue來簡化消費者與生產者的問題 2007-04-28 14:28 | TiGERTiAN
    哈哈。。抄襲抄襲。文檔上面的不詳細,寫個能運行得出來。。當作學習。。  回復  更多評論
      
    # re: 使用BlockingQueue來簡化消費者與生產者的問題 2008-12-01 15:47 | qw
    public Object take() throws InterruptedException {
    // TODO Auto-generated method stub
    while(isEmpty()){}
    return list.remove(0);
    }

    while(isEmpty()){},這個代碼你也敢寫?  回復  更多評論
      
    # re: 使用BlockingQueue來簡化消費者與生產者的問題 2008-12-01 15:58 | TiGERTiAN
    @qw
    怎么了?是一個無限循環,主要是有一個生產線程在那里,我才這樣寫的,如果是實際開發當然要做相應處理,不能這樣寫了。  回復  更多評論
      
    主站蜘蛛池模板: 久久久久亚洲av无码专区蜜芽 | 99在线热播精品免费99热| 一个人在线观看视频免费| 67pao强力打造67194在线午夜亚洲| 黄色三级三级免费看| 又粗又大又长又爽免费视频| 亚洲AV无码一区二区乱子仑| 国产精品酒店视频免费看| 精品国产亚洲一区二区三区在线观看 | 9277手机在线视频观看免费| 2022年亚洲午夜一区二区福利 | 九一在线完整视频免费观看| 国产亚洲精品线观看动态图| 国产麻豆一精品一AV一免费| 666精品国产精品亚洲| 搡女人真爽免费视频大全| 久久精品熟女亚洲av麻豆| 亚洲国产小视频精品久久久三级 | 美女黄网站人色视频免费国产| 亚洲av无码专区在线| 麻豆国产精品入口免费观看| 黄色a三级三级三级免费看| 亚洲中文字幕久久精品无码喷水 | 午夜影视在线免费观看| 黄页网址大全免费观看12网站| 中文字幕亚洲无线码| 91香蕉国产线观看免费全集| 亚洲一卡2卡3卡4卡乱码 在线| 又粗又硬免费毛片| 可以免费观看的毛片| 亚洲xxxx18| 亚洲精品国产V片在线观看| 日韩av无码久久精品免费| 亚洲午夜无码久久| 国产成人精品久久亚洲| 1000部禁片黄的免费看| 免费夜色污私人影院网站电影 | 亚洲福利视频一区二区三区| 免费看的一级毛片| 最新国产乱人伦偷精品免费网站| 亚洲色偷偷色噜噜狠狠99|