⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ostest.java

📁 Java版本的操作系中的生产者与消费者的代码~
💻 JAVA
字号:
/**
 * @(#)OPtest.java
 *
 *
 * @author 
 * @version 1.00 2008/4/14
 */
import java.util.*;
public class OsTest{
	public static void  main(String args[]){
		Container contain=new Container();
		Producer p1= new Producer(contain,"p1");
		Producer p2= new Producer(contain,"p2");
	    Producer p3= new Producer(contain,"p3");
		Consumer c1= new Consumer(contain,"c1",p1);
		Consumer c2= new Consumer(contain,"c2",p3,p2,p1);
		Thread Pone= new Thread(p1);
		Thread Ptow= new Thread(p2);
		Thread Pthree=new Thread(p3);
		Thread Cone= new Thread(c1);
		Thread Ctow= new Thread(c2);
		Pone.start();
		Cone.start();
		Ptow.start();
		Ctow.start();
		Pthree.start();
	}
	}
class ExceptionQueueFull extends Exception{ 
 ExceptionQueueFull(String msg){
super(msg); 
}
}
class ExceptionQueueEmpty extends Exception{ 
ExceptionQueueEmpty(String msg){
super(msg); 
}
}
class Production{
	String name;
        Producer p;
	public Production(Producer p) {
		this.name="产品"+p.nameP;
	}
	public String getname(){
		return name;
	}
}
class Container {
	int maxContent=5;
    int f=0;//队首元素的位置
    int r=0;//队尾元素的位置
    Boolean sign_C=new Boolean(true);
    Boolean sign_P=new Boolean(true);
	public Production [] content=new Production[maxContent];
	public int getsize(){
		return(r-f+maxContent)%maxContent;
	}
	public boolean isEmpty(){
		return (f==r);
	}
        public boolean isFull(){
                 return getsize()==maxContent-1;   
          } 
	public synchronized void  push(Production pro) throws ExceptionQueueFull {
            if(!this.isFull())
            {   content[r]=pro;
                r=(r+1)%maxContent;
            }else throw new ExceptionQueueFull("错误:队列已满!");
	}
	public synchronized Production  pop() throws ExceptionQueueEmpty{
            if(this.isEmpty())
            throw new ExceptionQueueEmpty("错误:队列空");
		Production firstone=content[f];
		content[f]=null;
                f=(f+1)%maxContent ;        
		return firstone ;
}
   public void show(){
   	System.out.println("缓冲区的情况如下:");
   	for(int i=0;i<content.length;i++){
   		if(content[i]==null)
   			System.out.println("缓冲池"+(i+1)+"号区:null");
   			else System.out.println("缓冲池"+(i+1)+"号区:"+content[i].getname());
   		}
   	}
}
 class Consumer extends Thread{
     Container contain;
     String nameC;
     Producer [] args;
    public Consumer(Container contain,String nameC,Producer...args){
    	this.nameC=nameC;
		this.contain=contain;
         this.args=args;
         System.out.println("消费者"+nameC+"依次所需要的商品的生产者是:");
         for(Producer b:args)
         System.out.print(b.nameP+" ");
         System.out.println();	
	}
	public boolean  check(Producer per){
		//检查队列中是否含有所需求的商品
		synchronized (contain){  	  	
		int m=this.contain.getsize();
		  for(int i=0;i<m;i++){   
		  		 while(contain.isEmpty())
            {
                try{			
                     //上锁
                    contain.wait();
                    // 加入到这个资源的等待队列,并释放
		}catch(InterruptedException e){
                    e.printStackTrace();
                }
            }
            try{ Production temp =this.contain.pop();
            if(per.tion==temp){
              System.out.println("消费者"+nameC+"消费了"+per.tion.name);
              return true ; 
              	}
             	else {
             	try{this.contain.push(temp);
             	}catch(ExceptionQueueFull e){
             		}
             		}
            }catch(ExceptionQueueEmpty e){
            }
	}
	}
	return false ;
	}
    private void consume(){
    	System.out.println("现在消费者"+nameC+"进行消费");
            for(Producer a:args){
            if(!check(a))	{
             System.out.println("在已生产的产品中没有所需求的"+a.tion.name);
             System.out.println("唤醒生产者"+a.nameP+"进行生产");
             	if (contain.isFull()){
             		System.out.println("但是队列已满,现在进行强制删除一个产品,再进行订造") ;
             		try{Production delet=contain.pop();
             		System.out.println("强行删除了"+delet.name);
             		}catch(ExceptionQueueEmpty e){}
             			}
            a.produce();
             check(a);
   
             } 
         }
         contain.show();
         		  try{
            synchronized(contain){
              this.contain.notify();
            	sleep(1000);
            	}
                
            }catch(InterruptedException e){
		e.printStackTrace();}	
    }
 
    @Override
    public void run(){
       
        while(true) { 
        consume();
         contain.show();
        }
    }
}
class Producer extends  Thread{
	 Container contain;
	 String nameP;
     Production tion;
	public Producer(Container contain,String nameP){
		this.contain=contain;
		this.nameP=nameP;
		this.tion=new Production(this);
	}
     void produce()
    {
            //假如满了
            while(contain.isFull())
            {
                try
                {synchronized(contain)
                 {contain.wait();// 加入到这个资源的等待队列,并释放锁 
                 }
                }
                catch(InterruptedException e)
                {
                     e.printStackTrace();
                }
            }
            try {
            contain.push(tion);
            System.out.println("生产者"+nameP+"生产一个产品");
            }catch(ExceptionQueueFull e){
            }
            try{ 
            synchronized(contain){
                contain.notify();
                sleep(1000);
            }
            }catch(InterruptedException e){
                e.printStackTrace();
            }
    }
        
    
    @Override
    public void run(){
        //不断地生产
       
        while(true)  {
        produce();
         contain.show();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -