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

📄 admonitor.java

📁 a simple java mutlti thread concurrency program src code
💻 JAVA
字号:
import java.util.Vector;
import Utilities.*;

class ADmonitor extends MyObject {
	private int group_size;
	private boolean isDragonPlay = false; //playing dice
	//private int num
	private Object conveyC = null; //4 clerk queue;
	private Object conveyG = null; //4 out shop group
	private Vector waitingAdventurers = new Vector();
	private Vector outshopGroups = new Vector();
	private int outshopAdv =0; //out shop adventurers
	private int avClerks; //available clerk number
	private int numClerks; 
	
	public ADmonitor(int group_size,int numClerks){
		this.group_size=group_size;
		conveyC = new Object();
		avClerks = numClerks;
		this.numClerks = numClerks;
		
		
	}
	
	//public synchronized void meetDragon() {
	public void meetDragon() { //!!!solve deadlock
		
		Object convey = new Object();
		synchronized(convey){
			if (isDragonBusy(convey))
				while(true)//wait to be notified,not interrupted
					try{convey.wait();break;}
					catch(InterruptedException e){continue;}
		}
	}
	
	private synchronized boolean isDragonBusy(Object convey){
		boolean status;
		if(isDragonPlay){
			  waitingAdventurers.addElement(convey);
			  status = true;
		  
		}else{ 
			isDragonPlay = true;//enter cave 4 play dice
			status = false; //skip while loop
		}
		return status;
	}
	
	public synchronized void leaveDragon() {
		isDragonPlay = false;
		if(waitingAdventurers.size()>0){
			synchronized (waitingAdventurers.elementAt(0)){
				waitingAdventurers.elementAt(0).notify();
			}
			waitingAdventurers.removeElementAt(0);
			isDragonPlay = true;//already notify next
		}
			
	}
	public synchronized void loseDragon() {
		isDragonPlay = false;
		if(waitingAdventurers.size()>0){
			synchronized (waitingAdventurers.elementAt(0)){
				waitingAdventurers.elementAt(0).notify();
			}
			waitingAdventurers.removeElementAt(0);
			isDragonPlay = true;//already notify next
			
			Object convey = new Object();//go back to end of line
			waitingAdventurers.addElement(convey);

			while(true)
			try{convey.wait();break;
			}catch(InterruptedException e){continue;}
		}		
		
	}
	 public synchronized void enterShop() {
		 
		 if (avClerks<0||outshopGroups.size()>0){//otherwise enter shop
			 if (outshopAdv==group_size){//need make new group
				 conveyG = new Object();
				 outshopGroups.addElement(conveyG);
				 outshopAdv =0; //new group init
			 }
			 if (conveyG!=null&&outshopAdv<group_size){
				 outshopAdv++;
				 while(true)
				 try{conveyG.wait();break;}
				 catch(InterruptedException e){continue;}
			 }
			 
			
		 }
		 
		 
	 }
	 
//	 public void leaveShop(int i) {
//	      Object convey = new Object();
//	      synchronized (convey) {
//	      }
//	      
//	 }
	 
	 public void findClerk() {
	      synchronized (conveyC) {//take a clerk or wait behind other adventurers in shop
	    	  avClerks --;
	    	  if (avClerks<0){
	    		  while(true){
	    			  try{
	    				  conveyC.wait();
	    				  break;
	    			  }catch(InterruptedException e){//handle notify()after interrupt() race condition
	    				  if(avClerks>=0)break; //notify()after interrupt
	    				  else continue;  //interrupted so continue waiting
	    			  }
	    		  }
	    	  }
	      
	      }
	      
	 }
	 
	 public void leaveClerk() {
	     synchronized (conveyC) {//leave a clerk,signal a waiting adventurer
	    	  avClerks ++;
	    	  if (avClerks<=0){
	    		  conveyC.notify();
	 
	    	  }
	          if (avClerks ==numClerks&&outshopGroups.size()>0){ //???4 new group coming in shop
	        	  synchronized(outshopGroups.elementAt(0)){ 
	        		  outshopGroups.elementAt(0).notifyAll();
	        	  }
	        	  outshopGroups.removeElementAt(0);
	          }
	      }
	 }
	
}

⌨️ 快捷键说明

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