Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package com.ru.usty.elevator; import java.util.ArrayList; import java.util.concurrent.Semaphore; /** * The base function definitions of this class must stay the same * for the test suite and

package com.ru.usty.elevator; import java.util.ArrayList; import java.util.concurrent.Semaphore; /** * The base function definitions of this class must stay the same * for the test suite and graphics to use. * You can add functions and/or change the functionality * of the operations at will. * */ public class ElevatorScene { //TO SPEED THINGS UP WHEN TESTING, //feel free to change this. It will be changed during grading public static final int VISUALIZATION_WAIT_TIME = 500; //milliseconds private int numberOfFloors; private int numberOfElevators; ArrayList personCount; //use if you want but //throw away and //implement differently //if it suits you ArrayList exitedCount = null; public static Semaphore exitedCountMutex; //Base function: definition must not change //Necessary to add your code in this one public void restartScene(int numberOfFloors, int numberOfElevators) { /** * Important to add code here to make new * threads that run your elevator-runnables * * Also add any other code that initializes * your system for a new run * * If you can, tell any currently running * elevator threads to stop */ this.numberOfFloors = numberOfFloors; this.numberOfElevators = numberOfElevators; personCount = new ArrayList(); for(int i = 0; i < numberOfFloors; i++) { this.personCount.add(0); } if(exitedCount == null) { exitedCount = new ArrayList(); } else { exitedCount.clear(); } for(int i = 0; i < getNumberOfFloors(); i++) { this.exitedCount.add(0); } exitedCountMutex = new Semaphore(1); } //Base function: definition must not change //Necessary to add your code in this one public Thread addPerson(int sourceFloor, int destinationFloor) { /** * Important to add code here to make a * new thread that runs your person-runnable * * Also return the Thread object for your person * so that it can be reaped in the testSuite * (you don't have to join() yourself) */ //dumb code, replace it! personCount.set(sourceFloor, personCount.get(sourceFloor) + 1); return null; //this means that the testSuite will not wait for the threads to finish } //Base function: definition must not change, but add your code public int getCurrentFloorForElevator(int elevator) { //dumb code, replace it! return 1; } //Base function: definition must not change, but add your code public int getNumberOfPeopleInElevator(int elevator) { //dumb code, replace it! switch(elevator) { case 1: return 1; case 2: return 4; default: return 3; } } //Base function: definition must not change, but add your code public int getNumberOfPeopleWaitingAtFloor(int floor) { return personCount.get(floor); } //Base function: definition must not change, but add your code if needed public int getNumberOfFloors() { return numberOfFloors; } //Base function: definition must not change, but add your code if needed public void setNumberOfFloors(int numberOfFloors) { this.numberOfFloors = numberOfFloors; } //Base function: definition must not change, but add your code if needed public int getNumberOfElevators() { return numberOfElevators; } //Base function: definition must not change, but add your code if needed public void setNumberOfElevators(int numberOfElevators) { this.numberOfElevators = numberOfElevators; } //Base function: no need to change unless you choose // not to "open the doors" sometimes // even though there are people there public boolean isElevatorOpen(int elevator) { return isButtonPushedAtFloor(getCurrentFloorForElevator(elevator)); } //Base function: no need to change, just for visualization //Feel free to use it though, if it helps public boolean isButtonPushedAtFloor(int floor) { return (getNumberOfPeopleWaitingAtFloor(floor) > 0); } //Person threads must call this function to //let the system know that they have exited. //Person calls it after being let off elevator //but before it finishes its run. public void personExitsAtFloor(int floor) { try { exitedCountMutex.acquire(); exitedCount.set(floor, (exitedCount.get(floor) + 1)); exitedCountMutex.release(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //Base function: no need to change, just for visualization //Feel free to use it though, if it helps public int getExitedCountAtFloor(int floor) { if(floor < getNumberOfFloors()) { return exitedCount.get(floor); } else { return 0; } } } The problem People enter a building and wish to use an elevator to travel between floors. Each elevator is implemented as a thread and each person is implemented as a thread. At any given time the system will include a separate thread for each elevator and each person that has entered the system but not yet finished its run by exiting at the floor it wishes to travel to. The thread should wait at the appropriate places and unlock certain lock at the appropriate times, to make sure that other threads can also conclude their run correctly. In the design work you can assume that the elevator threads are already running and that an outside source starts the person threads and lets them know where they are and where they wish to go. You still need to design the running functionality of the threads. Following are the different levels of complexity: Single elevator, two floors, everyone enters at the bottom floor and exits at the top floor. Single elevator, any number of floors, everyone enters at the bottom floor and exit at any other floor. The person-thread knows from the beginning at shich floor it wishes to exit. Single elevator, two floors, persons can enter at either floor and exit at the other floor. Single elevator, any number of floors, persons can enter and exit at any floor. The person-thread knows from the beginning at shich floor it wishes to exit. Any number of elevators, any number of floors, persons enter and exit at any floor. Persons enter at a floor and can use any elevator to travel. Each system can be described either for a single person in an elevator at a time, or for 6 people at a time in an elevator. You can decide when this level of complexity is added, but in the end each system should allow for 6 people in an elevator at a time. Feel free to add any intermediate levels of complexity if this helps you get your head around the problem. You can also skip levels in the final document if you feel they are included in other levels, but during the design process take care not to add too much at a time. QUESTION: 1) the semaphores that will be made and used in the system, 2) the events (elevator stops on floor, elevator leaves floor, person enters floor, etc.) that happen in the system and 3) what operations are called on the semaphores when these events occur. Any images and system descriptions are also welcome.

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

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

More Books

Students also viewed these Databases questions

Question

Understand how customers respond to effective service recovery.

Answered: 1 week ago

Question

Know the principles of effective service recovery systems.

Answered: 1 week ago