消费者生产者模型-java 线程模拟

   这段时间一直跟我导师做一个本科班的java课助教,每天都向对那些无聊的问题,好烦!今天终于见到他们要做一个有难度一点的题目了:
Solve a single producer, single consumer problem using wait( ) and notify( ). The producer must not overflow the receiver's buffer, which can happen if the producer is faster than the consumer. If the consumer is faster than the producer, then it must not read the same data more than once. Do not assume anything about the relative speeds of the producer or consumer.

   下面是相应的代码,关键采用了多线程,这个例子不不仅允许一个生产者和一个消费者,任意多个都可以。
 
 
// 产品类
class  Produce
{
    
private  String name;
    
    
public  Produce(String name)
    {
        
this .name = name;
    }
    
    
public  String toString()
    {
        
return  name;
    }
}
 
 
 
/**  仓库类*内部采用数组来表示循环队列,以存放产品 */
class  Storage
{

    
private   static    int  CAPACITY = 11 ;
    
private  Produce[]
    data
= new  Produce[CAPACITY];
    
private   int   front = 0 ;
    
private   int      rear = 0 ;
    
public  Storage( final   int  capacity)
    {
        
this .CAPACITY = capacity + 1 ;
    }
    
/** *消费一个产品 */
    
public  Produce
    Consume()
throws  InterruptedException
    {
        
synchronized ( this )
        {
            
while (front == rear)
            {
                
this .wait();
            }
            
int  access = front;
            front
= (front + 1 + CAPACITY) % CAPACITY;
            Produce produce 
= data[access];
            System.out.println(
" Consumer[ " +  Thread.currentThread().getName() + " ] consume: " + produce);
            System.out.println(
" Storage condition: count= " + (rear + CAPACITY - front)  % CAPACITY);
            
this .notify();
            
return  produce;
        }
    }
    
/** *生产一个产品 */
    
public   void  Produce(Produce produce) throws  InterruptedException
    {
        
synchronized ( this )
        {
            
while ((rear + 1 ) % CAPACITY  ==  front)
            {
                
this .wait();
            }
            data[rear]
= produce;                  rear = (rear + 1 % CAPACITY;
            System.out.println(
" Producer[ " + Thread.currentThread().getName() + " ] produce: " +  produce);
            System.out.println(
" Storage condition: count= " + (rear + CAPACITY - front)  % CAPACITY);
            
this .notify();
        }
    }
}
/**
*生产者类,采用线程,模拟生产者的行为
*/
class Producer extends Thread
{
private Storage storage;
private static int produceName=0;
  
    public Producer(Storage storage,String name)
    {
      super(name);
      this.storage=storage;
      }
    public void run()
    {
     
      while(true)
      {
      try
       {
          Produce produce=new Produce((++produceName)+"");
          storage.Produce(produce);
        
         sleep(100);
       } 
       catch(InterruptedException ie)
       {
         ie.printStackTrace();
         break;
       }
      }
    }
}
/**
*消费者,采用线程,模拟消费者行为
*/
class Consumer extends Thread
{
private Storage storage;
  
    public Consumer(Storage storage,String name)
    {
      super(name);
      this.storage=storage;
   
    }
   
	public void run()
    {
    Produce produce;
      while(true)
      {
      try
       {
       produce=storage.Consume();
     
      
         sleep(500);
       }
       catch(InterruptedException ie)
       {
         ie.printStackTrace();
         break;
       }
      }
    }
}
public class ProducerConsumer
{
public static void main(String[] args)
{
   Storage storage=new Storage(10);
   Producer producer1=new Producer(storage,"producer_01");
    Producer producer2=new Producer(storage,"producer_02");
   Consumer consumer1=new Consumer(storage,"consumer_01");
   Consumer consumer2=new Consumer(storage,"consumer_02");
   Consumer consumer3=new Consumer(storage,"consumer_03");
   Consumer consumer4=new Consumer(storage,"consumer_04");
   Consumer consumer5=new Consumer(storage,"consumer_05");
   producer1.start();
   producer2.start();
   consumer1.start();
   consumer2.start();
   consumer3.start();
   consumer4.start();
   consumer5.start();
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值