var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-20738293-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script')"/>
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 凌晨风 阅读(2502) 评论(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)

我参与的团队

随笔分类

随笔档案

文章分类

文章档案

新闻分类

新闻档案

收藏夹

围脖

最新随笔

搜索

  •  

最新评论

阅读排行榜

评论排行榜