Homework Help Question & Answers

[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
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 Coin

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
  • 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 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)...

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

  • NEED STUDENT TEST CLASS JAVA

    Create a test class. It will read in students' info from a file. The first line of the file contains a number indicating how many students information are specified inthe file. And the rest in the file contains information of each individual student. The test class will receive the name of the file through command line argument,create an array to store students info, read in the information from the file, and print out these students' info via calling the toString...

  • 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 *****************************************************/...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • 1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System....

    1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...

  • fill in the blanks with java code The following code implements a Java timer that looks...

    fill in the blanks with java code The following code implements a Java timer that looks like the application below. . Once the application starts it keeps counting seconds until it is terminated. Timer (sec): 20 You are given a partial implementation of the program. Fill out the missing code to complete the program. import java.awt.*; import java.awt.event.; import java.util."; import javax.swing. *; import javax.swing. Timer; * This program shows a timer that is updated once per second. public class...

  • ble and instance variable, and then use clas 4. Please discuss the difference between class variable...

    ble and instance variable, and then use clas 4. Please discuss the difference between class variable and instant variable to complete the following code for the count of number of stud public class Student _//q1: fill this line to create a class variable num. public Student 1/ 22: fill this line to increase num. public static int getNum() return num; public static void main(String[] args) { Student stl=new Student O; Student st2-new Student ; Student st3-new Student ; //q3: fill...

Free Homework 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.