/* * DiningPhilosopherObs.java (Implements DP using Event Notification) * * Written by : Nitin Jeevan Motgi (nmotgi@cs.ucf.edu) * * Implementation of EN in DP. * * Portions copyright(c) 2001 to School of Electrical Engineering and * Computer Science, UCF, Orlando. * * Use and distribution of this source code are strictly governed by * terms and conditions set by the authors. * * $Id : DiningPhilosopher.java, v1.0.0 02/10/2001. $ * * Revision History Revisited. */ import java.lang.*; import java.util.*; /***************************************************************************** * This class simulates a higher level interface for dining philosopher * problem using Event Notifications. This class basically Observable, so * when some changes happen in the Observable all the Observers that is * the philosophers will be notified about the events that are taking place * in the Dining Philosopher * * NOTE : Event Notification implementation requires that there should be * only one philosopher active(picking,eating and releasing). ****************************************************************************/ public class DiningPhilosopherObs extends Observable{ PhilosopherObs PhilSet[]; /* Set of Philosophers @Array.*/ int nEventCount; /* # of Events.*/ long lStartTime; /* @Start time of this sim.*/ long lStopTime; /* Intermediate @stop times.*/ int nPhil; /* # of Philosophers.*/ int nEvents; /* Max number of events.*/ ObsState State; /* State of Observable class.*/ int nSubEvents; /* This is default constructor for DiningPhilosopherObsproblem.*/ public DiningPhilosopherObs(int nPhil, int nEvents, long lStartTime){ this.nPhil = nPhil; this.nEvents = nEvents; this.lStartTime = lStartTime; /* Create all Philosopher needed for simulation.*/ PhilSet = new PhilosopherObs[nPhil]; /* Initialise Event count to zero for this simulation.*/ nEventCount = 0; nSubEvents = 1; State = new ObsState(); }/* End of Constructor.*/ /* This is standard Start procedure used to create all the Philosophers and also waits till all the threads die a grace full death. It has time management facility too.*/ public void Start(){ /* Time formatter is instantiated and ready for use.*/ TimeFormat Time = new TimeFormat(); /* Print the Start time for simulation.*/ lStopTime = System.currentTimeMillis(); System.out.println("Started using Event Notification at time " + Time.formatTime(lStopTime - lStartTime)); /* Create all philosophers.*/ for(int nIndex=0; nIndex < nPhil; nIndex++){ PhilSet[nIndex] = new PhilosopherObs((nIndex+1),this); addObserver(PhilSet[nIndex]); PhilSet[nIndex].start(); /* Start the threads immediately.*/ }/* End of For.*/ /* Start the first philosopher to eat.*/ State.Set(1); setChanged(); notifyObservers((Object)State); /* Wait till all the Philosophers die.*/ boolean bAllDead = false; while(bAllDead == false){ bAllDead = true; for(int nIndex=0; nIndex < nPhil; nIndex++){ if(PhilSet[nIndex].isAlive() == true) bAllDead = false; }/* End of for.*/ }/* End of while.*/ lStopTime = System.currentTimeMillis(); System.out.println("Completed using Event Notification at time " + Time.formatTime(lStopTime - lStartTime)); }/* End of Start.*/ /* Monitor Eat: This function first checks whether it is the first in in the request queue. If it is it will go ahead with picking the fork, eating and leaving the fork procedure. But, in case if it is not the one to whoes ID is in the queue it will go in the wait state till it has been notified with the one that has finished Phil Proc.*/ public synchronized void Eat(){ nEventCount++; /* PICK UP THE FORKS PRESENT ON YOUR ADJACENT SIDES AND ATTEMPT TO EAT. Note : There is no other Philosopher in this region, as the request queue Id match allows only one philosopher to be in this region of code.*/ /* Sync. and increment the event count.*/ if(nEventCount == nEvents){ State.Set(9999); setChanged(); notifyObservers((Object)State); return; }/* End if.*/ if(nSubEvents == nPhil) nSubEvents = 1; else nSubEvents++; /* Send Next Philosopher who will come to eat.*/ State.Set(nSubEvents); setChanged(); notifyObservers((Object)State); return; }/* End of Eat.*/ }/* End of DiningPhilosopherObs.*/