var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-20738293-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script')"/>
<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

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

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

    jutleo
    歡迎走進有風的地方~~
    posts - 63,  comments - 279,  trackbacks - 0
    package Thread;

    public class ProducerConsumer 
    {
        
    public static void main(String[] args) 
        
    {
            SynchronizedStack ss 
    = new SynchronizedStack();
            Producer p 
    = new Producer(ss); //產生一個生產者
            Consumer c = new Consumer(ss); //產生一個消費者
            new Thread(p).start(); //啟動生產者線程
            new Thread(c).start(); //啟動消費者線程
            
        }


    }


    class Bread //定義生產面包
    {
        
    int id;
        Bread(
    int id)
        
    {
            
    this.id = id;
        }

        
        
    public String toString() //重寫toString方法
        {
            
    return "bread :" + id;
        }

    }


    class SynchronizedStack //定義一個盛面包的容器
    {
        
    int index = 0;
        Bread[] bread 
    = new Bread[6];
        
        
    public synchronized void putIn(Bread bread) // 放進方法
        {
            
    while(index == this.bread.length)
            
    {
                
    try 
                
    {
                    
    this.wait(); // 只針對synchronized
    //Object對象的wait()方法,表示調用當前對象的wait()方法,運行當前對象的此方法的線程等待,直到被notify()喚醒
                }
     
                
    catch (InterruptedException e) 
                
    {
                    e.printStackTrace();
                }

            }

            
    this.notify(); //喚醒一個wait的線程
    //        this.notifyAll();//喚醒所有wait的線程
            this.bread[index] = bread;
            index 
    ++;
        }

        
        
    public synchronized Bread putOut() // 拿出方法
        {
            
    while(index == 0)
            
    {
                
    try 
                
    {
                    
    this.wait();
                }
     
                
    catch (InterruptedException e) 
                
    {
                    e.printStackTrace();
                }

            }

            
    this.notify(); //喚醒一個wait的線程
    //        this.notifyAll();//喚醒所有wait的線程
            index --;
            
    return bread[index];
        }

    }


    class Producer implements Runnable
    {
        SynchronizedStack ss 
    = null;
        Producer(SynchronizedStack ss)
        
    {
            
    this.ss = ss;
        }

        
    public void run() 
        
    {
            
    for(int i=0;i<30;i++)
            
    {
                Bread bread 
    = new Bread(i);
                ss.putIn(bread);
                System.out.println(
    "生產了 :" + bread);
                
    try
                
    {
    //                Thread.sleep(1000);
                    Thread.sleep((int)Math.random() * 1000);
                }

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

            }

        }

    }


    class Consumer implements Runnable
    {
        SynchronizedStack ss 
    = null;
        Consumer(SynchronizedStack ss)
        
    {
            
    this.ss = ss;
        }

        
    public void run() 
        
    {
            
    for(int i=0;i<30;i++)
            
    {
                Bread bread 
    = ss.putOut();
                System.out.println(
    "消費了 :" + bread);
                
    try
                
    {
    //                Thread.sleep(1000);
                    Thread.sleep((int)Math.random() * 1000);
                }

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

            }

        }

    }
    posted on 2007-12-10 19:48 凌晨風 閱讀(2517) 評論(4)  編輯  收藏 所屬分類: Java學習筆記

    FeedBack:
    # re: 生產者消費者問題(以面包為例)
    2007-12-10 21:35 | wǒ愛伱--咾婆
    呵呵..支持了...好東東...寫的不錯啊..  回復  更多評論
      
    # re: 生產者消費者問題(以面包為例)
    2007-12-11 00:28 | zsulzm
    這樣貌似有問題吧
      回復  更多評論
      
    # re: 生產者消費者問題(以面包為例)
    2008-04-02 15:15 | 不行吧
    吃面包的時候不能往里面放面包的。  回復  更多評論
      
    # re: 生產者消費者問題(以面包為例)[未登錄]
    2008-04-28 15:29 | z
    有點問題,輸出固定不夠明顯.Consumer和Producer中不必都有 try
    {
    // Thread.sleep(1000);
    Thread.sleep((int)Math.random() * 1000);
    }
    catch(InterruptedException e)
    {
    e.printStackTrace();
    }
    去掉一個,就明顯多了.  回復  更多評論
      

    <2007年12月>
    2526272829301
    2345678
    9101112131415
    16171819202122
    23242526272829
    303112345

    常用鏈接

    留言簿(11)

    我參與的團隊

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    新聞分類

    新聞檔案

    收藏夾

    圍脖

    最新隨筆

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 男女超爽视频免费播放| 欧美色欧美亚洲另类二区| 免费看黄的成人APP| 亚洲综合色视频在线观看| 丰满少妇作爱视频免费观看| 亚洲国产成人久久综合碰| 全部一级一级毛片免费看| 亚洲毛片av日韩av无码| 成人在线视频免费| 亚洲国产精品成人精品小说 | 亚洲黄色在线视频| 9277手机在线视频观看免费| 噜噜噜亚洲色成人网站∨| 青娱乐免费在线视频| 久久精品国产亚洲AV| 亚洲日韩精品无码专区网站| 国产婷婷成人久久Av免费高清| 亚洲va国产va天堂va久久| 成年人视频免费在线观看| 亚洲砖码砖专无区2023| 亚洲Av无码乱码在线播放| 曰批全过程免费视频在线观看无码 | 国产成人免费片在线视频观看| 国产亚洲视频在线播放大全| 中文字幕不卡亚洲| 久久成人国产精品免费软件| 亚洲国产一区二区三区在线观看| 亚洲毛片网址在线观看中文字幕| 日本卡1卡2卡三卡免费| 亚洲一区二区三区免费视频| 国产在线98福利播放视频免费| 中文在线观看永久免费| 亚洲youjizz| 亚洲一级特黄无码片| 91久久精品国产免费直播| 国产成人综合久久精品亚洲| 亚洲AV福利天堂一区二区三| 毛片免费全部播放一级| 东北美女野外bbwbbw免费| 最新亚洲精品国偷自产在线| 亚洲中文字幕无码爆乳AV|