我的漫漫程序之旅

专注于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 々上善若水々 阅读(754) 评论(0)  编辑  收藏 所属分类: J2SE


只有注册用户登录后才能发表评论。


网站导航: