java模拟实现生产者---消费者问题

本文章为小编原创,请尊重文章的原创性,转载请注意写明转载来源:http://blog.csdn.net/u012116457


已知技术参数:
生产者消费者问题,描述一组生产者向一组消费者提*品/消息。它们共享一个有界缓冲区,生产者向其中放产品/消息,消费者从中取产品/消息。只要缓冲区未满,生产者可放产品/消息,只要缓冲区有数据,消费者可取消息。即应满足下列二个同步条件:
1.只有在缓冲池中至少有一个缓冲区已存入消息后,消费者才能从中提取消息,否则消费者必须等待。
2.只有缓冲池中至少有一个缓冲区是空时,生产者才能把消息放入缓冲区,否则生产者必须等待。

设计要求:
要求设定一个缓冲池中有n个缓冲区,每个缓冲区存放一个消息,创建多个生产者,消费者,并在每个生产者消费者创建时、发出放/取产品申请时、正在放/取产品时和放/取产品结束时分别给出提示信息,并显示取/方产品前后的缓冲区状态,以检查所有处理都遵守相应的操作限制。


上代码:

最核心的代码:

package kcsj;
/**
 * 模拟实现生产者--消费者问题
 * 
 * @date 2014/06/24
 *
 */
public class ProductiveConsumption {
	private int front=0;             //队头
	private int next=0;              //队尾
	private int bufferLength;        //缓冲区大小
	private String buffer[];         //缓冲区
	private int emptyNum;          //空缓冲区数目
	public ProductiveConsumption(int bufferLength){
		this.bufferLength=bufferLength;
		buffer=new String[bufferLength];
		emptyNum=bufferLength;
	}
	//生产
	public synchronized void produce(String data){
		System.out.println("生产前,空缓冲区数目-----------"+emptyNum);
		System.out.println("***生产者正在生产"+data);
		while(full()){
			System.out.println("*****缓冲池已满,生产等待");
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.notify();
		buffer[next]=data;
		next=(next+1)%bufferLength;
		System.out.println("****生产者成功生产:"+data);
		emptyNum--;
		System.out.println("生产后,空缓冲区数目-----------"+emptyNum);
	}
	//消费
	public synchronized void consum(){
		System.out.println("消费前,空缓冲区数目-----------"+emptyNum);
		while(empty()){
			System.out.println("*****缓冲池为空,消费等待");
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("***消费者正在消费"+buffer[front]);
		this.notify();
		System.out.println("****消费者成功消费:"+buffer[front]);
		front=(front+1)%bufferLength;
		emptyNum++;
		System.out.println("消费后,空缓冲区数目-----------"+emptyNum);
	}
	//缓冲池是否已满
	public boolean full(){
		if(emptyNum==0){
			return true;
		}
		return false;
	}
	//缓冲池是否为空
	public boolean empty(){
		if(bufferLength==emptyNum){
			return true;
		}
		return false;
	}

}



其他辅助代码:

package kcsj;
/**
 *创建生产者
 */ 
public class CreateProducer implements Runnable{
	ProductiveConsumption pc;
	int producerNum;
	public CreateProducer(ProductiveConsumption pc,int producerNum){
		this.pc=pc;
		this.producerNum=producerNum;
	} 
	public void run(){
		for(int i=0;i<producerNum;i++){
			Producer producer=new Producer(pc);
			try {
				Thread.sleep((int)(Math.random()*100));
			} catch (InterruptedException e) {
				e.printStackTrace();
			} 
		}
	}
}

package kcsj;
/**
 *创建消费者
 */ 
public class CreateConsumer implements Runnable{
	ProductiveConsumption pc;
	int consumerNum;
	public CreateConsumer(ProductiveConsumption pc,int consumerNum){
		this.pc=pc;
		this.consumerNum=consumerNum;
	} 
	public void run(){
		for(int i=0;i<consumerNum;i++){
			Consumer consumer=new Consumer(pc);
			try {
				Thread.sleep((int)(Math.random()*100));
			} catch (InterruptedException e) {
				e.printStackTrace();
			} 
		}
	}
}

package kcsj;

public class Producer{
	ProductiveConsumption pc;
	public Producer(ProductiveConsumption pc){
		this.pc=pc;
		System.out.println("*成功创建一个生产者");
		apply();
	} 
	public void apply(){
		char c=(char)(Math.random()*26+'A');  
		String data=String.valueOf(c);
		System.out.println("**生产者发出请求");
		pc.produce(data);
	}

}

package kcsj;

public class Consumer{
	ProductiveConsumption pc;
	public Consumer(ProductiveConsumption pc){
		this.pc=pc;
		System.out.println("*成功创建一个消费者");
		apply();
	} 
	public void apply() {
		System.out.println("**消费者发出请求");
		pc.consum();
	}
}

package kcsj;

import java.util.Scanner;

public class Test {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		System.out.println("输入缓冲区大小");
		int buffLength=sc.nextInt();
		System.out.println("输入生产者和消费者个数");
		int prodecerNum=sc.nextInt();
		int consumerNum=sc.nextInt();

		ProductiveConsumption pc=new ProductiveConsumption(buffLength); 
		Runnable cp=new CreateProducer(pc,prodecerNum); 
		Runnable cc=new CreateConsumer(pc,consumerNum); 
		Thread t1=new Thread(cp); 
		Thread t2=new Thread(cc);
		t1.start(); 
		t2.start(); 
	}
}


java模拟实现生产者---消费者问题,布布扣,bubuko.com

java模拟实现生产者---消费者问题

上一篇:Java的socket服务UDP协议


下一篇:python基础教程_学习笔记14:标准库:一些最爱——re