Page 829 - Introduction to Programming with Java: A Problem Solving Approach
P. 829

                Apago PDF Enhancer
Figure A9.1 Top level of a program that simulates a simple ecosystem
This class drives the classes in Figures A9.2, A9.3, and (A9.4 or A9.5a and A9.5b).
The class in Figure A9.1 is the top level of a program that describes the interaction of a predator like a fox and prey like a group of field mice. In this example, the prey gets its food continuously from ever-present vegetation, while the predator gets its food intermittently by eating prey when predator and prey happen to meet. The prey is one thread. The predator is another thread. Notice how this driver starts both prey and predator threads. These threads represent the parallel lives of these creatures in their ecosystem.
The prey and predator threads are objects. There is also another object, called encounter. This object represents an ongoing intermittent relationship between the predator and the prey. In this relation- ship, some of the prey come into the presence of the predator, and the predator eats them. Presumably the predator eats only part of the prey in each particular encounter, and in the interim the prey continuously replenish by reproducing and eating vegetation. In the encounter relationship, the prey provides food, and the predator consumes food. So computer folks like us might say the prey thread is a producer thread, and the predator thread is a consumer thread. Of course, the prey also “consume” vegetation, so if our model included a relationship between the field mice and the vegetation they eat, in that context we could call our prey thread a “consumer” thread. So the terms “producer” and “consumer” should not be associated abso- lutely to any one thread.
Any relationship between threads violates the ideal of “thread independence.” It complicates the lives of real creatures, and it complicates a program that simulates them.
Figure A9.2 shows the class that describes prey threads. Notice that it does extend the Thread class. There is just one instance variable, a reference to the encounter relationship—field mice are undoubtedly aware of their unpleasant relationship with a fox. The zero-parameter constructor assigns a default name to all
Appendix 9 Multithreading 795
 /**************************************************************
*
Ecosystem.java
Dean & Dean
Driver for a simple predator/prey (consumer/producer) system.
The predator and prey objects are separate threads, and
encounter is an object that describes their relationship.
**************************************************************/
*
*
*
*
*
public class Ecosystem
{
}
// end Ecosystem class
public static void main(String[] args)
{
}
Encounter encounter = new Encounter(prey, predator);
// start threads
prey.start();
predator.start();
// end main
Prey prey = new Prey();
Predator predator = new Predator();
// producer thread
// consumer thread






























































   827   828   829   830   831