All pastes #885970 Raw Edit

Untitled

public text v1 · immutable
#885970 ·published 2008-01-31 08:48 UTC
rendered paste body
public class Alternate 
{
	private static Object monitor = new Object();
	private static Object doneMonitor = new Object();
	public static String[] halls = ResidentInput.getHallNames();
	public static int[] rooms = ResidentInput.getHallRooms();
	public static String[] enrollRequests;
	public static String name;
	
	public static void main (String[] args) 
	{
		Alternate a = new Alternate();
		
		Thread t1 = new Thread(new Alternator("Tom", monitor, doneMonitor));
		Thread t2 = new Thread(new Alternator("Jerry", monitor, doneMonitor));
		
		t1.start();
		t2.start();
	}
}


















public class Alternator implements Runnable 
	{
		public String name;
		public Object monitor;
		public Object doneMonitor;
		
		public Alternator(String name, Object monitor, Object doneMonitor) 
		{
			this.doneMonitor = doneMonitor;
			this.monitor = monitor;
			this.name = name;
			
			if (name.equalsIgnoreCase("Tom"))
				Alternate.enrollRequests = ResidentInput.getEnrollRequestsTom();
			if (name.equalsIgnoreCase("Jerry"))
				Alternate.enrollRequests = ResidentInput.getEnrollRequestsJerry();
		}
		
		public void run() 
		{
				synchronized(monitor) 
				{			
					for(int x = 0;x < Alternate.enrollRequests.length;x++)
					{
		    			boolean found = false;
		    			String currentReq = Alternate.enrollRequests[x];
		    			for(int y = 0;y < Alternate.halls.length && found == false;y++)
		    			{
		    				if (currentReq.equalsIgnoreCase(Alternate.halls[y]))
		    				{
		    					found = true;
		    					if (Alternate.rooms[y]>0)
		    					{
		    						Alternate.rooms[y]--;
		    						System.out.println(name + " - " + currentReq + " - OK");
		    					}
		    					else
		    						System.out.println(name + " - " + currentReq + " - REJECTED");
		    				}
		    			}
		    			if (found == false)
		    			{
		    				System.out.println(name + " - " + currentReq + " - NONEXIST");
		    			}
					monitor.notifyAll();
					try 
					{
						monitor.wait();
					} 
					catch (InterruptedException e) {}
				}
			}
			synchronized(doneMonitor) 
			{
				doneMonitor.notifyAll();
			}
		}
	}