Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Step: 3

blur-text-image

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

Advanced Database Systems For Integration Of Media And User Environments 98

Authors: Yahiko Kambayashi, Akifumi Makinouchi, Shunsuke Uemura, Katsumi Tanaka, Yoshifumi Masunaga

1st Edition

9810234368, 978-9810234362

More Books

Students also viewed these Databases questions

Question

discuss the main antecedents and consequences of sport involvement

Answered: 1 week ago