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

版权声明:本文为博主原创文章,承蒙转载请注明作者和出处 https://blog.csdn.net/linyt/article/details/568111
   这段时间一直跟我导师做一个本科班的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();
  }
}
阅读更多

扫码向博主提问

海枫

博客专家

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • 网络
  • Kernel
  • 编程语言
  • 系统设计
去开通我的Chat快问

没有更多推荐了,返回首页