關(guān)于生產(chǎn)者與消費者之間的問題,其實在你準(zhǔn)確理解了線程的概念和方法的用法后,就很容易理解了。

          以下是偶寫的一個關(guān)于生產(chǎn)者與消費者的程序,看看對你理解有沒有幫助。

          //生產(chǎn)者
           class Producer implements Runnable{
           private WOWOTou wwt=null;
           private Basket bs=null;
           private static int id=1;
           private static boolean state=true;
           
           Producer(Basket bs){
            this.bs=bs;
           }
           
           public  WOWOTou produce(){
            try {
             Thread.sleep( (long) (Math.random()*8000));
            } catch (InterruptedException e) {
             
             e.printStackTrace();
            }
            wwt=new WOWOTou(id);

            //輸出調(diào)試語句
            System.out.println();
            System.out.println(Thread.currentThread().getName()+"生產(chǎn)了id為"+id+"的包子");  

            id++;
            return wwt;
           }

           public void push(){
            bs.push(wwt);
            wwt=null;
           }
           
           public void run() {
           
            while(state){
             synchronized(this){
              produce();
              push();
             }
            
            }
            
           }
           
          }




          //消費者
          class Custmer implements Runnable{
           
           private Basket bs=null;
           private WOWOTou wwt=null;
           private static boolean state=true;
           
           public Custmer(Basket bs) {
            this.bs = bs;
           }
           
            
           public  void cousme(){
            try{
             Thread.sleep((long) (Math.random()*8000));
            }catch(InterruptedException e){
             e.printStackTrace();
            }

            // 輸出調(diào)試語句
            System.out.println();
            System.out.println(Thread.currentThread().getName()+"消費id為"+wwt.getWOWOTouID()+"的包子");  

            wwt=null;
            
           }

           public void pop(){
            wwt=bs.pop();
           }
           
           public void run() {
            while(state){
             synchronized(this){
              pop();
              cousme();
             }  
            }
            
           }


          }

           


          //裝WOWOTou的Basket
          class Basket{
           
           private int index=0;
           private  final static int max=5;
           private WOWOTou wwt=null;
           private WOWOTou[] wwtbs=new WOWOTou[5];
           private Producer p=null;
           private static Basket bs=null;
           
           private Basket(){}
           
           public synchronized static Basket getBasket(){
            if(bs==null)
             bs=new Basket();
            
            return bs;
           }
           
           
           public synchronized void  push(WOWOTou wwt){
            
            while(index==max){
             System.out.println("包子滿了,趕緊吃包子");
             try {
              this.wait();
             } catch (InterruptedException e) {    
              e.printStackTrace();
             }
            }
             this.notify();

           //輸出調(diào)試語句
             System.out.println();
             System.out.println("這是"+Thread.currentThread().getName()+"生產(chǎn)的id為"+wwt.getWOWOTouID()+"的包子");
             
            wwtbs[index++]=wwt;       
           }
           
           public synchronized WOWOTou pop(){
            while(index==0){
             System.out.println();
             System.out.println("沒包子了,趕緊生產(chǎn)包子");
             try{
              this.wait();
             }catch(InterruptedException e){
              e.printStackTrace();
             }
            }
             this.notify();
             if(index>0)
              index--;

            //輸出調(diào)試語句
             System.out.println();
             System.out.println(Thread.currentThread().getName()+"拿到id為"+wwtbs[index].getWOWOTouID()+"的包子"); 

            return wwtbs[index];
           }
          }

           


          class WOWOTou{
           
           private int id=0;
           
           WOWOTou(int id){
            this.id=id;
            
           }
           
           public int getWOWOTouID(){
            return id;
           }
          }



          當(dāng)你明白線程后,就應(yīng)該考慮多線程和線程的性能問題,盡量減小鎖的粒度,但要注意死鎖問題。
          posted on 2008-10-20 20:55 李威威 閱讀(443) 評論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
           
          主站蜘蛛池模板: 始兴县| 监利县| 曲靖市| 宜丰县| 介休市| 白河县| 梅州市| 七台河市| 新邵县| 博野县| 通化市| 临湘市| 琼海市| 兴业县| 尼木县| 刚察县| 海晏县| 凤冈县| 天门市| 砀山县| 司法| 嘉义市| 鲁甸县| 德州市| 右玉县| 乐东| 周口市| 霍邱县| 泾川县| 万宁市| 凉城县| 重庆市| 苏州市| 天等县| 察雅县| 崇信县| 原平市| 上虞市| 丹江口市| 兴山县| 平江县|