OS实验三_生产者消费者

1.consumer:
package myPackage;
public class Consumer implements Runnable{
	  private int id;
	    private Storage storage;
	    private Storage sarr[];
	    private int n;
	    public Consumer (Storage sarr[],int n){
	          this.sarr=sarr;
	          this.n=n;
	    }
	    public void run(){
	        while(true){
	            try{
	            	if(n==1) {
	            		storage=sarr[0];
	            		storage.consume(storage);
	            	}
	            	else if(n>1) {
	            		storage=new Storage();
	            		storage.consume(sarr, n);
	            	}
	                Thread.sleep(1000);
	            }catch (InterruptedException e){
	                e.printStackTrace();
	            }
	        }
	    }
}
2.producer
package myPackage;
import java.util.LinkedList;//由链表构成的数组

public class Producer implements Runnable{

	    private int id;
	    
	    private Storage storage;
          
	    private Storage sarr[];
	    
	    private int n;

	    public Producer (Storage sarr[],int n){
	          this.sarr=sarr;
	          this.n=n;

	    }
	    
	    public void run(){
	        while(true){
	            try{
	            	if(n==1) {
	            		storage=sarr[0];
	            		storage.produce(storage);
	            	}
	            	else if(n>1) {
	            		storage=new Storage();
	            		storage.produce(sarr, n);
	            	}
	                Thread.sleep(1000);
	            }catch (InterruptedException e){
	                e.printStackTrace();
	            }
	        }
	    }
}
3.storage 
package myPackage;
import java.util.LinkedList;
public class Storage {//缓冲区,如果一个缓冲池有n个缓冲区:Storage s[n]=new Storage()[10];
   private static int MAX_SIZE=10;
   private LinkedList<Object> list=new LinkedList<>();//一个缓冲区可以放多个产品
   
   public LinkedList<Object> returnList(){
	   return list;
   }
   
   public int returnSize(){
	   return MAX_SIZE;
   }
   
   public Storage(){
	   
   }
   
   public void produce(Storage sarr[],int n) {//多缓冲区,n表示缓冲区数量
		  int i=0;
		  Storage storage=sarr[0];
			for(i=0;i<n;i++) {
				LinkedList<Object> list=sarr[i].returnList();
				if(list.size() + 1 <MAX_SIZE) {//缓冲区未满
					storage=sarr[i];
					break;
				}
			}
			if(i==n) {//所有缓冲区均满
		        storage=sarr[0];
			}
		  
	    	LinkedList<Object> list=storage.returnList();
	        synchronized (list) {
	            while (list.size() + 1 > 10) {
	                System.out.println("【生产者" + Thread.currentThread().getName()
			                + "】仓库已满");
	                try {
	                    list.wait();
	                } catch (InterruptedException e) {
	                    e.printStackTrace();
	                }
	            }
	            list.add(new Object());
	            System.out.println("【生产者" + Thread.currentThread().getName()
	                    + "】生产一个产品,仓库["+i+"]现库存" + list.size());
	            list.notifyAll();
	        }
	    }
   

   public void produce(Storage storage) {//单缓冲区
   	//LinkedList<Object> list=storage.returnList();
       synchronized (storage.list) {
           while (storage.list.size() + 1 > 10) {
               System.out.println("【生产者" + Thread.currentThread().getName()
		                + "】仓库已满");
               try {
               	storage.list.wait();
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
           storage.list.add(new Object());
           System.out.println("【生产者" + Thread.currentThread().getName()
                   + "】生产一个产品,现库存" + storage.list.size());
           storage.list.notifyAll();
       }
   }
   

   public  void consume(Storage sarr[],int n) {//多缓冲区
	    int i=0;
	    Storage storage=sarr[0];
		for(i=0;i<n;i++) {
			LinkedList<Object> list=sarr[i].returnList();
			if(list.size()>=0) {
				storage=sarr[i];
				break;
			}
		}
		if(i==n) {
			 storage=sarr[0];
		}
   	//LinkedList<Object> list=storage.returnList();
       synchronized (storage.list) {
           while (storage.list.size() == 0) {
               System.out.println("【消费者" + Thread.currentThread().getName() 
						+ "】仓库为空");
               try {
               	storage.list.wait();
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
           storage.list.remove();
           System.out.println("【消费者" + Thread.currentThread().getName()
                   + "】消费一个产品,仓库["+i+"]现库存" + storage.list.size());
           storage.list.notifyAll();
       }
   }
   
   public  void consume(Storage storage) {
   	//LinkedList<Object> list=storage.returnList();
       synchronized (storage.list) {
           while (storage.list.size() == 0) {
               System.out.println("【消费者" + Thread.currentThread().getName() 
						+ "】仓库为空");
               try {
               	storage.list.wait();
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
           storage.list.remove();
           System.out.println("【消费者" + Thread.currentThread().getName()
                   + "】消费一个产品,现库存" + storage.list.size());
           storage.list.notifyAll();
       }
   }

   
}
4. MyMain
 package myPackage;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class MyMain {
	
	  private static Storage []sarr=new Storage[10];
	  
	  public static void electsence() {
	    	System.out.println("1.单生产者单消费者单缓冲区");
	    	System.out.println("2.多生产者多消费者单缓冲区");
	    	System.out.println("3.多生产者多消费者多缓冲区");

	    	System.out.println("请选择问题应用场景:");
	    	Scanner scan = new Scanner(System.in);
	    	int choice=scan.nextInt();
	    	
	    	switch(choice) {
	    	case 1:sence1();break;
	    	case 2:sence2();break;
	    	case 3:sence3();break;
	    	}
	    }
	    public static void sence1() {
	    	System.out.println("================单生产者单消费者单缓冲区================\n");
	   
	    	sarr[0]=new Storage();
	        Thread p1 = new Thread(new Producer(sarr,1));
	        Thread c1 = new Thread(new Consumer(sarr,1));
	        p1.start();
	        c1.start();
	    }
	    
	    public static void sence2() {
	    	System.out.println("================多生产者多消费者单缓冲区================\n");
	    	sarr[0]=new Storage();
	    	System.out.println("请输入生产者个数:");
	    	int pnum=0;
	    	while(true) {
	    	Scanner scan=new Scanner(System.in);
	    	pnum=scan.nextInt();
	    	if(pnum<=1) {
	          System.out.println("输入生产者数不可小于等于1!请重新输入:");
	    	}
	    	else break;
	    	}
	    	
	    	System.out.println("请输入消费者个数:");
	    	int cnum=0;
	    	while(true) {
	    	Scanner scan=new Scanner(System.in);
	    	cnum=scan.nextInt();
	    	if(cnum<=1) {
	          System.out.println("输入消费者数不可小于等于1!请重新输入:");
	    	}
	    	else break;
	    	}
	    	
	    	for(int i=0;i<pnum;i++) {
	    		Thread p=new Thread(new Producer(sarr,1));
	    		p.start();
	    	}
	    	
	    	for(int j=0;j<cnum;j++) {
	    		Thread c=new Thread(new Consumer(sarr,1));
	    		c.start();
	    	}
	    }
	    public static void sence3() {
	    	System.out.println("================多生产者多消费者多缓冲区================\n");
	    	System.out.println("请输入生产者个数:");
	    	int pnum=0;
	    	while(true) {
	    	Scanner scan=new Scanner(System.in);
	    	pnum=scan.nextInt();
	    	if(pnum<=1) {
	          System.out.println("输入生产者数不可小于等于1!请重新输入:");
	    	}
	    	else break;
	    	}
	    	
	    	System.out.println("请输入消费者个数:");
	    	int cnum=0;
	    	while(true) {
	    	Scanner scan=new Scanner(System.in);
	    	cnum=scan.nextInt();
	    	if(cnum<=1) {
	          System.out.println("输入消费者数不可小于等于1!请重新输入:");
	    	}
	    	else break;
	    	}
	    	
	    	System.out.println("请输入缓冲区个数:");
	    	int snum=0;
	    	while(true) {
	    	Scanner scan=new Scanner(System.in);
	    	snum=scan.nextInt();
	    	if(snum<=1) {
	          System.out.println("输入缓冲区数不可小于等于1!请重新输入:");
	    	}
	    	else break;
	    	}
	    	
	    	for(int i=0;i<snum;i++) {
	    		sarr[i]=new Storage();
	    	}
	    	
	    	for(int j=0;j<pnum;j++) {
	    		Thread p=new Thread(new Producer(sarr,snum));
	    		p.start();
	    	}
	    	
	    	for(int j=0;j<cnum;j++) {
	    		Thread c=new Thread(new Consumer(sarr,snum));
	    		c.start();
	    	}
	    }
	
	
	  public static void main(String[] args) {
		  electsence();
	  }
}
posted @ 2022-05-10 12:43  亚1918  阅读(34)  评论(0编辑  收藏  举报