<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

    主站蜘蛛池模板: 免费成人在线电影| 亚洲视屏在线观看| 欧美激情综合亚洲一二区| 久久久久久精品免费免费自慰| 国产成人精品久久亚洲高清不卡 | 亚洲av无码片vr一区二区三区 | 三上悠亚在线观看免费| 免费中文字幕在线| 青青视频免费在线| 亚洲人成网站色在线入口| 亚洲国产aⅴ成人精品无吗| 在线观看免费人成视频色9| 亚洲中文字幕无码av在线| 无码国产精品一区二区免费虚拟VR| 亚洲欧洲日产韩国在线| 中国在线观看免费国语版| 亚洲白嫩在线观看| 亚洲中文无码永久免费| 亚洲午夜福利在线视频| 日本一道一区二区免费看| 国产成人亚洲精品播放器下载| 亚洲精品456播放| 女人隐私秘视频黄www免费| 国产亚洲无线码一区二区 | 久久国产精品免费看| 亚洲欧洲国产日韩精品| 综合在线免费视频| 国产精品自拍亚洲| 国产成人毛片亚洲精品| 免费无码一区二区三区| 亚洲国产成人久久77| 日本一道本高清免费| 成全在线观看免费观看大全 | 一区免费在线观看| 亚洲国产精品lv| 成人免费777777| 一区二区三区在线观看免费| 亚洲影院在线观看| 午夜神器成在线人成在线人免费| 国产精品成人亚洲| 亚洲视频一区二区在线观看|