Question: To the following code, add this part into it demonstrate the behavior of threads that share data, and use synchronized methods. You may use wait/

To the following code, add this part into it

demonstrate the behavior of threads that share data, and use synchronized methods. You may use wait/ notify/ notifyAll in this exercise.

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

make sure all exceptions are handled.

Animal.java

import java.util.concurrent.ThreadLocalRandom;

public class Animal implements Runnable {

private String name; private int position; private int speed; private int restMax; private static boolean winner = false;

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public int getPosition() { return position; }

public void setPosition(int position) { this.position = position; }

public int getSpeed() { return speed; }

public void setSpeed(int speed) { this.speed = speed; }

public int getRestMax() { return restMax; }

public void setRestMax(int restMax) { this.restMax = restMax; }

public static void main(String[] args) {

Animal rabbit = new Animal(); rabbit.setName("Rabbit"); rabbit.setPosition(0); rabbit.setRestMax(150); rabbit.setSpeed(5); Animal turtle = new Animal(); turtle.setName("Turtle"); turtle.setPosition(0); turtle.setRestMax(100); turtle.setSpeed(3); Thread rabbitThread = new Thread(rabbit); Thread turtleThread = new Thread(turtle);

rabbitThread.start(); turtleThread.start(); }

@Override public void run() {

while (position <= 100 && !winner) { position += speed; int rand = ThreadLocalRandom.current().nextInt(0, getRestMax()); System.out.println("Running: " + getName() + "\tPosition: " + getPosition()); if (position >= 100) { winner = true; System.out.println("Winner: " + getName()); }

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!