/* * PhilosopherMon.java (Implements Philosopher using Monitors) * * Written By : Nitin Jeevan Motgi (nmotgi@cs.ucf.edu) * * Implements Philosopher using Monitors. * * 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 author. * * Revision History. * 1. Created Basic Structure Nitin 02/10/2001. * 2. Documentation added. Nitin 02/10/2001. * 3. Final Documentation Check Nitin * 4. Final Variable Name Check Nitin * 5. Final Functionality Check Nitin */ /***************************************************************************** * This class basically simulates philosopher requests for eating using * Monitors. Basically it will make a request to acquire the lock before * eating. Once, the request gets into the queue it will make an attempt * to enter eating stage were it will pick forks and eat and also release * them after eating. *****************************************************************************/ class PhilosopherMon extends Thread{ public int nID; /* Indiviual Philosopher ID.*/ private DiningPhilosopherMon DPhil; /* Back Reference to higher class.*/ private boolean bEndCondition; /* End Condition indicator.*/ /* This is default constructor for PhilosopherMon.*/ PhilosopherMon(int nID,DiningPhilosopherMon DPhil){ this.nID = nID; this.DPhil = DPhil; bEndCondition = true; /* Initially set to true.*/ }/* End of PhilosopherMon.*/ /* This is "heart" of this code. It runs the loop till the bEndCondition returns false. The break from this loop is grace full death of the thread.*/ public void run(){ while (bEndCondition) { DPhil.AcquireLock(this); /* Make a request for Lock.*/ DPhil.Eat(this); /* Simulate Picking, Eating.*/ DPhil.ReleaseLock(this); /* Release the Lock.*/ }/* End of While.*/ }/* End of run.*/ /* Sets the End condition for grace full death of the loop.*/ public void SetEndCondition(boolean bState){ bEndCondition = bState; }/* End of SetEndCondition.*/ }/* End of PhilosopherMon.*/