Java Threads The following class implements a model of a stu
[Java] [Threads] The following class implements a model of a student dining hall serving pizzas to students. 10 pizzas are baked, then served to 20 students.
Students are numbered between 0 and 19 in the order they are served.
A message is printed indicating whether a student starved or was served a pizza.
(a.) Rewrite the DiningHall class so that after the makePizza( ) method is called 10 times,
the servePizza( ) method is called once each from 20 different threads.
(b.) Insert synchronization to eliminate data races in your code, if any exist.
(c.) Describe what data races may occur in your multithreaded code without synchronization.
public class DiningHall {
static int pizzaNum;
static int studentID;
public void makePizza() {
pizzaNum++;
}
public void servePizza() {
String result;
if (pizzaNum > 0) {
result = \"Served \";
pizzaNum--;
} else {
result = \"Starved \";
}
System.out.println(result + studentID);
studentID++;
}
public static void main(String[] args) {
DiningHall d = new DiningHall();
for (int i = 0; i < 10; i++) {
d.makePizza();
}
for (int i = 0; i < 20; i++) {
d.servePizza();
}
}
}
Solution
a.
public static void main(String[] args) {
DiningHall d = new DiningHall();
for (int i = 0; i < 10; i++) {
d.makePizza();
if (i == 9){
for (int j = 0;j < 20; j++)
d.servePizza();
}
}
}
![[Java] [Threads] The following class implements a model of a student dining hall serving pizzas to students. 10 pizzas are baked, then served to 20 students. St [Java] [Threads] The following class implements a model of a student dining hall serving pizzas to students. 10 pizzas are baked, then served to 20 students. St](/WebImages/6/java-threads-the-following-class-implements-a-model-of-a-stu-985407-1761506204-0.webp)
![[Java] [Threads] The following class implements a model of a student dining hall serving pizzas to students. 10 pizzas are baked, then served to 20 students. St [Java] [Threads] The following class implements a model of a student dining hall serving pizzas to students. 10 pizzas are baked, then served to 20 students. St](/WebImages/6/java-threads-the-following-class-implements-a-model-of-a-stu-985407-1761506204-1.webp)