Anda di halaman 1dari 17

4.

0 Question 4 - Modeling a Car Park


A car park is design and condition at a maximum capacity of 5, and at the same time disallows an empty car park. This means that the car park has to be occupied at all times. At least 1 car has to be parked in the car park at any given time. The car park limits car from entering anymore if the total capacity is exceeded after calculating the sum of cars present and new cars that wants to enter the car park. It can be deduced as well by comparing the number of spaces available in the car park with the number of cars that wants to enter the car park.

The cars can come into the car park anytime it wants, as long as there are available spaces in the car park. If there are more cars than the available spaces, the amount of car will be limited to the most possible spaces which can be accommodated to them. If there are no spaces, all cars are not allowed to enter the car park, and the cars will not wait but will end the process of waiting itself. It is assumed that they are going to another car park to park their car.

Other than that, the cars can leave anytime as well, as long as at least one car stays parked in the car park. If there are not enough cars to depart at a time, it will wait for cars to come park into the car park, and then the process of departing can take place. For example, the 4 cars want to depart, but there are only 2 cars in the car park. The car park waits for another 3 cars to park, and then 4 cars will only depart.

Thus, the important thing to note here is the condition of the special car park, and the mutual exclusion (mutex) technique is used to handle and control the threads that are running in the designed program. A solution is derived and executed for the programs functions.

4.1 Petri Net & Firing Table


4.1.1 Petri Net Car In Car park In

Car Ready To Enter


Blocked / Wait Car Entered

Available spaces for park


Carpark Occupied Enter

Car Ready To Park

Car Parked
Blocked / Wait Car Ready To Depart

Check Parking

At least 1 car in carpark

Car Departs

Car Exit

4.1.2 Firing Table

Car In Car Ready to Enter Car Ready to Park Car Ready to Depart Car Exit Available space for park At least 1 car in car park

Car Entered 1

Car Parked 0

Car Departs 0

Car park In 0

Car park occupied 0

Check Parking 0

4.2 Coding
4.2.1 Carpark.java package q4; /** * @author Joel Tay Chng Ee * @ID */ public class Carpark { //create and initialize variables //space -> available parkings or free slots in carpark //capacity -> maximum number of cars in carpark int space = 4; int capacity = 5, checkSpace = 0; I09003552

public synchronized void depart(int cars) throws InterruptedException { /*to check if amount of cars departing and number of spaces combined together *exceeds the capacity of the carpark */ checkSpace = space + cars;

try { //if spaces left after cars depart are more than allowed while (checkSpace > 4) {

System.out.println("\t\t\t\t\t\t1 car has to occupy the carpark at all times. \n\t\t\t\t\t\t\tAvailable spaces cannot exceed 4. "); Thread.sleep(500); wait(); } } catch(InterruptedException e) { e.printStackTrace(); }

checkSpace =

space + cars;

if(checkSpace < 5) { System.out.println("\t\t\t\t\t\t" + cars + " car(s) leave the carpark"); space += cars; System.out.println("\t\t\t\t\t\t" + space + " space(s) remaining in the carpark"); System.out.println("\t\t\t\t\t\t(Thread complete)<--\n"); notify(); }

public synchronized void enter(int cars) throws InterruptedException { try {

checkSpace = space - cars; while (checkSpace < 0) { Thread.sleep(500); wait(); } } catch(InterruptedException e) { e.printStackTrace(); } checkSpace = space - cars;

if(checkSpace >= 0) { System.out.println(cars + " car(s) entered the carpark"); space -= cars; System.out.println(space + " space(s) remaining in the carpark"); System.out.println("(Thread complete)<--\n"); notify(); }

public int getSpace() { return space; } }

This carpark.java is the mutex class which is used to control the thread by using the term synchronized, wait and notify. In this class, there are three main methods that can be executed in running the program. The randomly generated number will go into the relevant methods that accepts values and the output is produced accordingly. These numbers will be used for calculation in adding or subtracting spaces in a car park. This mutex will be controlling the thread to not allow the car park to be empty at any time. It is responsible to keep the available spaces in the car park to a maximum of 4. If there are 5 spaces in the car park, it means that no cars are in the car park as the maximum capacity of the car park is 5. Other than that, it does not allow cars to fill up more than the spaces available. The cars that are ready to enter the car park has to be lesser than the amount of free spaces. The mutex is also in charge of the waiting of depart() method. What it means is that if the method asks for a specific amount of cars to leave, it will wait till the sufficient amount of cars that are needed to be parked in the car park in order for the departing of cars to exit from the car park to happen. For example, if there are 2 cars in the car park and the method request for 3 cars to leave, the program will wait till there are 4 cars or more for the departing process to take place. The reason it is 4 rather than 3 because it needs to leave 1 car parked in the car park.

4.2.2 Enter.java package q4;

/** * @author Joel Tay Chng Ee * @ID */ public class Enter extends Thread { Carpark c; int carsEnter, carsSpaces, chkValid; I09003552

public Enter(Carpark c) { this.c = c; } public void run() { for(int i = 1; i < 5; i++) { //generates a random number between 1 to 5 carsEnter = (int)(Math.random()*5)+1; System.out.println("-->(Thread " + i + " start)\n" + carsEnter + " car(s) prepare to go into the carpark" ); try { carsSpaces = c.getSpace(); //check if cars entering are more than spaces available in the carpark chkValid = carsSpaces - carsEnter;

//if sufficient spaces are available if (chkValid >= 0) { Thread.sleep(500); c.enter(carsEnter); } else { //insufficient spaces in carpark - reduce to possible value //new value for amount of cars entering is stored in carsEnterNew int carsEnterNew = carsEnter; while( chkValid < 0) { carsEnterNew--; chkValid = carsSpaces - carsEnterNew; }

System.out.println( carsEnterNew + " car(s) can go in the car park"); System.out.println( (carsEnter - carsEnterNew) + " car(s) are blocked from entering the\n\t carpark due to limited spaces"); c.enter(carsEnterNew); Thread.sleep(500); } } catch(InterruptedException e) {

e.printStackTrace(); } } } }

Enter.java is a class where the program calls for to work it as a method. The Enter.java class does the calling of method from the mutex class. In this class, a random number will be generated to output the value for the method that will be called in mutex class by this class. Other than that, it is also responsible to detect and check whether the generated random value of cars entering into the car part fits with the spaces that are available in the car park. If it is not available, the class will do what is appropriate; blocking extra cars and letting the right amount of cars into the car park. The extra cars will be discarded depending on the spaces that are available in the car park. It will be not exceeding the maximum capacity of the car park. Besides that, the generated value will be accepted if spaces available are equal or more.

4.2.3 Exit.java package q4;

/** * @author Joel Tay Chng Ee * @ID */ I09003552

public class Exit extends Thread { Carpark c; // create and initialize variables int carsExit = 0, check_validity;

public Exit(Carpark c) { this.c = c; } public void run() { //call event for 4 times for(int i = 1; i < 5; i++) { //generates a random number between 1 to 5 carsExit = (int)(Math.random()*4+1); try {

System.out.println("\t\t\t\t\t\t" + "-->(Thread " + i + " start)\n" + "\t\t\t\t\t\t" + carsExit + " car(s) prepares to leave carpark.");

Thread.sleep(500); c.depart(carsExit);

catch(InterruptedException e) { e.printStackTrace(); } } } }

This is the exit class that manages the departures of the cars from the car park. A random generated value is produced as well to output the cars that are needed to depart from the car park. The value will be put into the method that is in the mutex class for calculation of cars available in the car park for departure. If the random value for departure of cars exceeds the real amount of cars that are parked in the car park, the mutex class will handle it by waiting for sufficient amount of cars in the car park for the departure to happen.

4.2.4 Main.java package q4;

/** * @author Joel Tay Chng Ee * @ID */ public class Main { public static void main(String[] args) { System.out.println("Arrivals" + "\t\t\t\t\tDepartures\n=============================================== ====================================="); //call appropriate classes Carpark main = new Carpark(); I09003552

Enter enter = new Enter(main); Exit exit = new Exit(main);

//start thread enter.start(); exit.start(); } }

This main class is responsible to call the appropriate dependent classes and declare threads. After that, it initialize both threads to run. These threads are synchronized at the mutex class.

Output Print Screen


Output 1

Output 1 as you can see, it does not reach build successful because last thread that is called by the depart() method in mutex class is unable to finish. Both depart() and enter() are looped 4 times each. Now, the thread is stuck on wait() which is in the coding of the depart() in mutex class. This is because it is waiting for resource which is the cars to continue coming in for the departure activity to happen. In this case, 3 prepares to leave car park (as stated in the screenshot), but there is only 1 car left in the car park. It has to wait for another 3 more cars to come in for the departure of the 3 cars to happen. To solve this issue, we just have to loop the enter() method more times than the current preset. This will then notify the depart() thread and will continue the whole car park process.

Output 2

For output 2, we can see that in the end, it has built successful with no errors. This is because in this case, the depart() method still has sufficient amount of cars in the car park. The random generated value suits just nice as well to subtract the value from the cars in car park. The scenario above shows that the random generated value is 1; and 2 cars (3 spaces) remaining. It is enough for the process to happen and execute completely.

Anda mungkin juga menyukai