Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Gaming Students Five students are sitting at a round table. In front of the students is a prototype of the Xbox Two. Between each pair
Gaming Students Five students are sitting at a round table. In front of the students is a prototype of the Xbox Two. Between each pair of students is one controller. Before an individual student can play the Xbox, he must have two controllers, one taken from the left, and one taken from the right. The students must find some way to share controllers so that they all get to play. You are going to simulate this problem. Start by creating a class called Xbox.java. In this class have an array called controllers, which contains Boolean variables stating if the controllers are available or not. Provide a public synchronized method called getController(int number) which allows a student to take a particular controller. Next create a class called Student as another class in your file. In this class your constructor should take in a number showing which student you are (1-5) and a reference to the Xbox like Student(int number, Xbox Xbox) and store those as class variables. Students should implement Runnable. In the run method a student should think about Java for a random time (up to 2000 ms), then try to play Xbox. When thinking print to the screen "Student X is thinking about Java". Once done thinking the student tries to get the controller on their left, and once they have that one, they try to get their controller on the right. Once they have both controllers, they play for a random time (up to 3000 ms) and repeat the process forever. Print a message to the screen showing which student this thread represents and a status message showing what action the student is attempting thinking about Java, waiting for left controller, waiting for right controller, playing Xbox). Each student should keep count of how many times they played Xbox. In the main method of Xbox, create 5 students and begin their threads to play to the Xbox. After starting all the threads, your main thread should wait for one minute. After the minute it up it should get the number of times each student has played Xbox and output a summary table of the results. You should then kill off the threads. Hint: it is unsafe to just stop them. I would suggest adding a stop flag in your students. It should be set to false to start and the main thread can change these to true when it is ready for them to be killed off. Have your student main loop run until the stop flag is true. Make sure your algorithm is not susceptible to deadlock. You should probably also do something to avoid starving students. Save your work for this part in a file named Xbox.java. Gaming Students Five students are sitting at a round table. In front of the students is a prototype of the Xbox Two. Between each pair of students is one controller. Before an individual student can play the Xbox, he must have two controllers, one taken from the left, and one taken from the right. The students must find some way to share controllers so that they all get to play. You are going to simulate this problem. Start by creating a class called Xbox.java. In this class have an array called controllers, which contains Boolean variables stating if the controllers are available or not. Provide a public synchronized method called getController(int number) which allows a student to take a particular controller. Next create a class called Student as another class in your file. In this class your constructor should take in a number showing which student you are (1-5) and a reference to the Xbox like Student(int number, Xbox Xbox) and store those as class variables. Students should implement Runnable. In the run method a student should think about Java for a random time (up to 2000 ms), then try to play Xbox. When thinking print to the screen "Student X is thinking about Java". Once done thinking the student tries to get the controller on their left, and once they have that one, they try to get their controller on the right. Once they have both controllers, they play for a random time (up to 3000 ms) and repeat the process forever. Print a message to the screen showing which student this thread represents and a status message showing what action the student is attempting thinking about Java, waiting for left controller, waiting for right controller, playing Xbox). Each student should keep count of how many times they played Xbox. In the main method of Xbox, create 5 students and begin their threads to play to the Xbox. After starting all the threads, your main thread should wait for one minute. After the minute it up it should get the number of times each student has played Xbox and output a summary table of the results. You should then kill off the threads. Hint: it is unsafe to just stop them. I would suggest adding a stop flag in your students. It should be set to false to start and the main thread can change these to true when it is ready for them to be killed off. Have your student main loop run until the stop flag is true. Make sure your algorithm is not susceptible to deadlock. You should probably also do something to avoid starving students. Save your work for this part in a file named Xbox.java
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started