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

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

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

    我的漫漫程序之旅

    專注于JavaWeb開發
    隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 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 22:41 々上善若水々 閱讀(790) 評論(0)  編輯  收藏 所屬分類: J2SE

    主站蜘蛛池模板: 国产成人亚洲精品电影| 国产免费拔擦拔擦8X高清在线人 | 黄床大片30分钟免费看| 亚洲精品国自产拍在线观看| 国产精品免费高清在线观看| 亚洲一区二区三区久久久久| 免费一级毛片女人图片| 成人性生交大片免费看好| 亚洲乱码一二三四区麻豆| 日韩亚洲精品福利| 五月婷婷在线免费观看| 无套内谢孕妇毛片免费看看| 久久亚洲AV成人无码软件| 国产婷婷高清在线观看免费| 久久这里只精品99re免费| 亚洲成AV人片在WWW| 无码乱人伦一区二区亚洲| 日本无卡码免费一区二区三区| baoyu116.永久免费视频| 亚洲人成人伊人成综合网无码| 亚洲人成网亚洲欧洲无码久久| 日韩免费a级毛片无码a∨| 十八禁在线观看视频播放免费| 亚洲αⅴ无码乱码在线观看性色| 亚洲大片在线观看| 亚洲日韩国产精品乱| 免费无码又爽又刺激聊天APP| 18禁超污无遮挡无码免费网站 | 亚洲熟女乱色一区二区三区| 亚洲精品二区国产综合野狼 | 亚洲精品视频专区| 亚洲第一网站男人都懂| 无码日韩人妻av一区免费| 国产免费一区二区视频| 一边摸一边桶一边脱免费视频| 亚洲一卡2卡3卡4卡乱码 在线| 亚洲av中文无码乱人伦在线播放 | 国产精品亚洲综合一区| 日本无卡码免费一区二区三区| 色婷婷7777免费视频在线观看| 日韩免费在线视频|