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

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

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

    我的漫漫程序之旅

    專注于JavaWeb開發(fā)
    隨筆 - 39, 文章 - 310, 評(píng)論 - 411, 引用 - 0
    數(shù)據(jù)加載中……

    生產(chǎn)者消費(fèi)者問題(以面包為例)

    package Thread;

    public class ProducerConsumer 
    {
        
    public static void main(String[] args) 
        
    {
            SynchronizedStack ss 
    = new SynchronizedStack();
            Producer p 
    = new Producer(ss); //產(chǎn)生一個(gè)生產(chǎn)者
            Consumer c = new Consumer(ss); //產(chǎn)生一個(gè)消費(fèi)者
            new Thread(p).start(); //啟動(dòng)生產(chǎn)者線程
            new Thread(c).start(); //啟動(dòng)消費(fèi)者線程
            
        }


    }


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

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

    }


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

            }

            
    this.notify(); //喚醒一個(gè)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(); //喚醒一個(gè)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(
    "生產(chǎn)了 :" + 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(
    "消費(fèi)了 :" + bread);
                
    try
                
    {
    //                Thread.sleep(1000);
                    Thread.sleep((int)Math.random() * 1000);
                }

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

            }

        }

    }


    posted on 2007-12-10 22:41 々上善若水々 閱讀(785) 評(píng)論(0)  編輯  收藏 所屬分類: J2SE

    主站蜘蛛池模板: www国产亚洲精品久久久| 69免费视频大片| caoporn国产精品免费| 精品亚洲福利一区二区| 亚洲熟伦熟女专区hd高清| 亚洲人成自拍网站在线观看| 亚洲日韩一区精品射精| 亚洲国产精品无码久久久秋霞1 | 免费人成年激情视频在线观看| 色视频色露露永久免费观看| 麻豆国产精品入口免费观看| 青青草国产免费久久久91| 国产传媒在线观看视频免费观看| 在线观看永久免费视频网站 | 97在线免费视频| 久久久久久一品道精品免费看| 久久久久久AV无码免费网站| 久久久久av无码免费网| 色窝窝免费一区二区三区| 永久免费AV无码网站在线观看| 国产在线a不卡免费视频| 又粗又大又长又爽免费视频| 中文字幕在亚洲第一在线| 亚洲av无码一区二区三区网站| 亚洲邪恶天堂影院在线观看| 亚洲国产成a人v在线| 亚洲成a人片在线不卡一二三区| 立即播放免费毛片一级| 中文字幕在线免费看| 91香蕉在线观看免费高清| 大地资源在线观看免费高清| 国产日产成人免费视频在线观看| 亚洲福利精品一区二区三区| 亚洲AV无码专区电影在线观看| 亚洲毛片无码专区亚洲乱| 欧美日韩亚洲精品| 精品一区二区三区高清免费观看| 日本免费大黄在线观看| 青青草免费在线视频| 亚洲日韩国产成网在线观看| 在线观看亚洲人成网站|