Question
Java Threads Part I Create a class called Animal that implements the Runnable interface. In the main method create 2 instances of the Animal class,
Java Threads
Part I
Create a class called Animal that implements the Runnable interface.
In the main method create 2 instances of the Animal class, one called rabbit and one called turtle. Make them "user" threads, as opposed to daemon threads.
Some detail about the Animal class. It has instance variables, name, position, speed, and restMax. It has a static boolean winner. It starts a false. The position represents the position in the race for this Animal object. The restMax represents how long the Animal rests between each time it runs.
The rabbit rests longer than the turtle, but the rabbit has a higher speed.
Let's make up some values to make this simulation more concrete.
The race is 100 yards. The initial position is 0. Suppose the speed of the rabbit is 5, and its maxRest is 150. Suppose the speed of the turtle is 3, and its maxRest is 100.
Adjust them so that the rabbit wins sometimes, and the turtle wins sometimes.
In the main method start both the rabbit and the turtle and see which one wins the races.
Here is the behavior of the run method in the Animal class.
Loop until the position is >= 100 or there is a winner. Each time through the loop, sleep() some random number of milliseconds. This random number will be between 0 and maxRest. Advance the position of the Animal by its speed.
Print who is running, what their position is, each time through the loop.
When someone wins, set the static variable winner to true, and both threads will finish their run method, and thus stop.
The winner is announced from inside the run method.
Part II
The objective here is to demonstrate the behavior of threads that share data, and use synchronized methods. You may use wait/ notify/ notifyAll in this exercise.
When the above race working, add to it in the following way.
Create a class called Food. It is not a Thread, and does not run. It's just a class that represents some data that will be shared by multiple threads.
Simulating an animal eating, simply means that the thread will sleep for some length of time. This is the same as the "resting" that the turtle an rabbit did in part I.
There is one instance of the Food class that is shared by both of the animals. Pass it to the constructor of the Animal class for both the turtle and the rabbit.
There is a method in the Food class called eat(). This method is synchronized, i.e., only one Animal can be eating at a time.
The rabbit eats the food (the thread will sleep) for a longer time than the turtle, thus giving an advantage to the rabbit.
But, the turtle must wait until the rabbit is done eating until it can eat, so the advantage is reduced.
Print out the message inside the eat method when the animal begins to eat, and when it is done eating. Indicate which animal it is that starts to eat.
Try making the eat method not synchronized, and observe the different behavior if the eat method allows the rabbit to begin eating before the turtle is done eating.
Include all exception handling. And use, synchronization, wait, notify and notifyAll for part 2
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started