com.avian.birds.philos
Class Phil2

java.lang.Object
  extended by javax.swing.SwingWorker<java.lang.String,java.lang.String>
      extended by com.avian.birds.basebirds.AbstractBasicBird
          extended by com.avian.birds.basebirds.StdBird
              extended by com.avian.birds.philos.Phil2
All Implemented Interfaces:
java.lang.Runnable, java.util.concurrent.Future<java.lang.String>, java.util.concurrent.RunnableFuture<java.lang.String>

public class Phil2
extends StdBird

A type of StdBird that is good at philosophizing but not so good at chatting. Each philosopher shares forks with the philosophers seated at both sides. The philosophers try to pick up the fork to their left and then try to get the fork to their right.

Rules:

Philosophers can only eat when they have both left and right forks.

When they has both forks, they eat for a random length of time and then put down both forks.

When they get the left fork, they try to pick up the righthand fork x number of times, where x is the number set in variable "getForkTries" (default is 3). This is an Xmv variable. On the Activity tab for each bird, you can set the number of times that each philosopher will try before giving up and putting down the left fork.

If a philosopher cannot get the righthand fork after x tries, they put down the lefthand fork and then waits for a random length of time before trying again.


Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.SwingWorker
javax.swing.SwingWorker.StateValue
 
Field Summary
 java.lang.String getForkTries
          Number of times this philosopher bird will try to get the second fork.
 
Fields inherited from class com.avian.birds.basebirds.StdBird
foodFromTree, healthStatus, jbRefStart, jlRefLastAte, jtaRefHistory, jtfRefHealth, mts, myTupleTree, nts, sampleXmv, startButtonText
 
Fields inherited from class com.avian.birds.basebirds.AbstractBasicBird
bhf, bif, birdDesc, birdHistory, birdName, bof, eatsFood, hatchTime, historyFileName, inputFileName, lastAte, levelOfInfo, lifeTime, napLength, outputFileName, stamina, stopNow, storesFood
 
Constructor Summary
Phil2()
           
 
Method Summary
 void digest()
          Provides the processing functionality for this Bird.
 void eatFood()
          Every Philosopher Bird must eat food to stay alive and this method provides the instructions that this Bird will follow to eat.
 void setGetForkTries(java.lang.String getForkTries)
          Setter that takes the value that was set on screen in Aviary and uses it to set the number of times that this philosopher bird will try to get second fork.
 void storeFood()
          Need to put back any forks that we are holding
 
Methods inherited from class com.avian.birds.basebirds.StdBird
checkAge, done, setHealthRef, setHealthStatus, setHistoryAreaRef, setJlRefLastAte, setStartButtonRef, setStartButtonText, setTupleTree
 
Methods inherited from class com.avian.birds.basebirds.AbstractBasicBird
addToBirdHistory, doInBackground, getAuxFood, getBirdDescription, getBirdHistory, getBirdName, getEatsFood, getHatchTime, getHistoryFileName, getInputFileName, getLastAte, getLevelOfInfo, getLifeTime, getNameOfFoodToStore, getNapLength, getOutputFileName, getStamina, isStopNow, setBhf, setBif, setBirdDesc, setBirdHistory, setBirdName, setBof, setEatsFood, setHatchTime, setHistoryFileName, setInputFileName, setLevelOfInfo, setLevelOfInfo, setLifeTime, setNapLength, setOutputFileName, setStamina, setStopNow, setStoresFood
 
Methods inherited from class javax.swing.SwingWorker
addPropertyChangeListener, cancel, execute, firePropertyChange, get, get, getProgress, getPropertyChangeSupport, getState, isCancelled, isDone, process, publish, removePropertyChangeListener, run, setProgress
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

getForkTries

public java.lang.String getForkTries
Number of times this philosopher bird will try to get the second fork. This is annotated as an Xmv (Externally Managed Variable) variable which means that it is detected by the Aviary and displayed on the Activity tab automatically. The value that the user enters is applied using the matching setter. If no value is entered by the user, the default value of 3 is used.

Constructor Detail

Phil2

public Phil2()
Method Detail

setGetForkTries

public void setGetForkTries(java.lang.String getForkTries)
Setter that takes the value that was set on screen in Aviary and uses it to set the number of times that this philosopher bird will try to get second fork. The name of this setter must match the Xmv variable's name.

Parameters:
getForkTries - is the int number of times that the philosopher bird will try to get the second fork. This value comes from Integer.parseInt() that will throw a NumberFormatException if a non-numeric value is passed as the param

eatFood

public void eatFood()
Every Philosopher Bird must eat food to stay alive and this method provides the instructions that this Bird will follow to eat. Because we're just trying to demonstrate deadlock prevention and not a full example, we are going to "borrow" the storesFood object so that we can specify which forks this philosopher bird will use

Overrides:
eatFood in class StdBird

digest

public void digest()
Provides the processing functionality for this Bird. However, we don't really process anything in the Dining Philosophers problem so, we are just going to delay and then return

Overrides:
digest in class StdBird

storeFood

public void storeFood()
Need to put back any forks that we are holding

Overrides:
storeFood in class StdBird