Question

[Java] [Threads] The following class implements a model of a student dining hall serving pizzas to...

[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();
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer: Program: public class DiningHall extends Thread static int pizzaNum; static int studentID public void makePizza ) pizSample Output: Before synchronizations: sh-4.3 java DiningHall Served e Served e Served 0 Starved 3 Starved 3 Starved 5 StarvAfter synchronizations: sh-4.3s java DiningHall Served e Starved 1 Starved 2 Starved 3 Starved 4 Starved 4 Starved 6 Starved

Copyable Code:

public class DiningHall extends Thread
{
static int pizzaNum;
static int studentID;
public void makePizza()
{
pizzaNum++;
}
public synchronized void run()
{
String result;
if (pizzaNum > 0)
{
result = "Served ";
pizzaNum--;
}
else
{
result = "Starved ";
}
if(studentID<20)
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 j = 0; j < 20; j++)
{
new DiningHall().start();
}
}
}
}

Add a comment
Know the answer?
Add Answer to:
[Java] [Threads] The following class implements a model of a student dining hall serving pizzas to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Language Java Step 1: Design a class called Student. The Student class should contain the following...

    Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration” program...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Create an abstract Student class for Parker University. The class contains fields for student ID number,...

    Create an abstract Student class for Parker University. The class contains fields for student ID number, last name, and annual tuition. Include a constructor that requires parameters for the ID number and name. Include get and set methods for each field; the setTuition() method is abstract. Create three Student subclasses named UndergraduateStudent, GraduateStudent, and StudentAtLarge, each with a unique setTuition() method. Tuition for an UndergraduateStudent is $4,000 per semester, tuition for a GraduateStudent is $6,000 per semester, and tuition for...

  • I need help in converting this into pseudo-code Student.java public class Student implements Comparable<Student>{ private String...

    I need help in converting this into pseudo-code Student.java public class Student implements Comparable<Student>{ private String name, major, status; private int rank;    public Student() { this.name = this.major = this.status = ""; this.rank = 0; }    public Student(String name, String major, String status) { this.name = name; this.major = major; this.status = status; } public String getName() { return name; } public String getMajor() { return major; } public String getStatus() { return status; }    public int...

  • I need to add a method high school student, I couldn't connect high school student to...

    I need to add a method high school student, I couldn't connect high school student to student please help!!! package chapter.pkg9; import java.util.ArrayList; import java.util.Scanner; public class Main { public static Student student; public static ArrayList<Student> students; public static HighSchoolStudent highStudent; public static void main(String[] args) { int choice; Scanner scanner = new Scanner(System.in); students = new ArrayList<>(); while (true) { displayMenu(); choice = scanner.nextInt(); switch (choice) { case 1: addCollegeStudent(); break; case 2: addHighSchoolStudent(); case 3: deleteStudent(); break; case...

  • A teacher wants to create a list of students in her class. Using the existing Student...

    A teacher wants to create a list of students in her class. Using the existing Student class in this exercise. Create a static ArrayList called classList that adds a student to the classList whenever a new Student is created. In the constructor, you will have to add that Student to the ArrayList. Coding below was given to edit and use public class ClassListTester { public static void main(String[] args) { //You don't need to change anything here, but feel free...

  • A teacher wants to create a list of students in her class. Using the existing Student...

    A teacher wants to create a list of students in her class. Using the existing Student class in this exercise. Create a static ArrayList called classList that adds a student to the classList whenever a new Student is created. In the constructor, you will have to add that Student to the ArrayList. Coding below was given to edit and use public class ClassListTester { public static void main(String[] args) { //You don't need to change anything here, but feel free...

  • Given a class called Student and a class called Course that contains an ArrayList of Student....

    Given a class called Student and a class called Course that contains an ArrayList of Student. Write a method called getDeansList() as described below. Refer to Student.java below to learn what methods are available. I will paste both of the simple .JAVA codes below COURSE.JAVA import java.util.*; import java.io.*; /****************************************************** * A list of students in a course *****************************************************/ public class Course{     /** collection of Students */     private ArrayList<Student> roster; /***************************************************** Constructor for objects of class Course *****************************************************/...

  • java create java class "nameperson" the example of output would be this: //if the user inputs...

    java create java class "nameperson" the example of output would be this: //if the user inputs sophia 206999109 2 //then the output shows this My name is None and my studentid is 0 My name is sophia and studentid is 0 My name is sophia and studentid is 206999109 My grade is 2 so i have to create two classes   class nameperson{ } class studentid extends person{ } class grade extends studentid{ } my main method is this class Main...

  • Writing 3 Java Classes for Student registration /** * A class which maintains basic information about...

    Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course {    /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT