Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java. 1. Create a class called Animal that implements the Runnable interface. 2. In the main method create 2 instances of the Animal class, one

Java.

1. Create a class called Animal that implements the Runnable interface.

2. 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.

3. Some detail about the Animal class. It has instance variables, name, position, speed, and restMax. It has a static boolean winner. It starts as 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.

4. 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.

I dont know if these rest times are interesting. Adjust them so that the rabbit wins sometimes, and the turtle wins sometimes.

5. In the main method start both the rabbit and the turtle and see which one wins the race.

6. 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.

Dont forget to add this part, Please!

The objective here is to demonstrate the behavior of threads that share data, and use synchronized methods. You do NOT use wait / notify / notifyAll in this exericse.

When the above race is working, add to it in the following way.

Create a class called Food. It is not a Thread, and does not run. Its 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 1.

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 a 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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 22nd European Conference Adbis 2018 Budapest Hungary September 2 5 2018 Proceedings Lncs 11019

Authors: Andras Benczur ,Bernhard Thalheim ,Tomas Horvath

1st Edition

3319983970, 978-3319983974

More Books

Students also viewed these Databases questions

Question

differentiate the function ( x + 1 ) / ( x ^ 3 + x - 6 )

Answered: 1 week ago