COSI 131a: Spring 2020 Programming Assignment 4
Due date is Monday, March 30th
Overview
After you implemented the primary system of a simulation of tunnels and vehicles in Programming Assignment (PA) 3, you will implement a higher-level tunnel controller that will schedule access
to the tunnels based on a priority system.
This assignment requires you to build on your synchronized code of PA3 and (1) add Java condition variables to avoid busy-waiting and (2) add a scheduler.
The Priority Scheduler
You have successfully programmed BasicTunnel in Programming Assignment 3, which enforces entry criteria and prevents race conditions. However, there are two problems with the design of BasicTunnel:
• While no tunnel is available to a Vehicle, that Vehicle busy-waits (loops over all tunnels repeatedly inside of its run() method).
• There is no way of prioritizing important vehicles (say, an ambulance) over less-important vehicles (say, an ice cream truck).
Priority Definition
In this assignment vehicle priority is defined as follows:
• Vehicle priority is a number between 0 and 4 (inclusive).
• A higher number means higher priority.
Implementation
You are provided with a class called PriorityScheduler (in PriorityScheduler.java) that implements Scheduler and you must add functionality that controls access to a collection
of Tunnels in order to implement the priority scheduling policy described above. The Tunnels “behind” the PriorityScheduler will be BasicTunnels. BasicTunnel and it should be the same class you implemented in PA3. You are not allowed to modify any classes in the Abstract package and your entire implementation should be in the PriorityScheduler.java.
PriorityScheduler will carry out the following tasks:
• Keep references to BasicTunnels as private member variables inherited from Scheduler inside PriorityScheduler.
• Enter tunnel (or wait): When a Vehicle calls admit(vehicle) on a PriorityScheduler instance, the following general steps should be executed:
o If vehicle.getPriority() is less than the highest priority from among all the other vehicles waiting to enter a tunnel (i.e., there is another vehicle with higher priority), then the vehicle must wait
o If there are no other vehicles with higher priority but there are no tunnels into which the vehicle can currently enter, then the vehicle thread must wait;
o Otherwise the vehicle successfully enters one of the tunnels.
• Exit tunnel: When a Vehicle v exits the scheduler by calling exit on a PriorityScheduler instance, the scheduler must call exitTunnel(v) on the appropriate tunnel from the collection of tunnels managed by the scheduler (remember, you may not modify Vehicle or any of the other classes provided to you, or BasicTunnel, so you must solve this problem within PriorityScheduler). After a vehicle has successfully exited a tunnel, the waiting vehicles should be signaled to retry to enter a tunnel. Note that the vehicles with highest priority should be allowed to enter.
• Use condition variables to avoid busy waiting when the car cannot find a tunnel to enter. Make sure the use of the condition variables is safe.
• You are not allowed to use the synchronized keyword (except in the BasicTunnel.java from PA3)
Tests: When this task is complete, your code should pass all tests within the PrioritySchedulerTest class (Car_Enter, Sled_Enter, Priority) included with this assignment. Please make sure to run the tests several times as a race condition problem can appear in only some of the runs. We provide the NUM_RUNS variable inside each test class that specifies how many times to run each test. It is set to 1 by default but feel free to change it to how many times you want to run each test.
You will need to provide JavaDoc documentation for any new classes, methods, or members you create, and you may be penalized if you fail to do so.
No “main” method
Just like in PA3, your only point of entry in the code we provide is through the JUnit tests, which will set up the environment in which your client threads will run. Your tasks for this assignment do not include writing a main method. Rather, you must rely on your understanding of condition variables, and the JUnit tests to know if you have completed the assignment.
Submission
Please export your project together with JavaDocs as described on LATTE and submit as a zip file. You should not submit source files individually.
Important Note about Disallowed Java Tools
• You may not use the thread priority methods provided by Java (e.g., you may not use Thread.setPriority).
• You should solve this assignment by using Java 8 and Junit 5. No external packages are allowed, unless you got permission from the TAs.