Lab 1 distinction assignment: Trainmonitoring
Deadlines
- See labs page
Lab description
In this assignment, you will implement a variation of the program to control the two trains that uses monitors.
Programming language
This lab assignment must be developed in Java.
Assignment
Your assignment is to adapt the program that controls the trains from the Lab 1: Trainspotting to use monitors instead of semaphores.
Requirements
-
All the requirements from lab 1 apply to this assignment as well, except that now you have to use monitors instead of semaphores.
-
You should use monitors that have explicit locks and condition variables. That is, you must use the
java.util.concurrent.locks
package from Java 5, notsynchronized
methods, to implement your monitor. -
You should not use the
tryLock()
method of theLock
interface. If you need such functionality, you have to implement it yourself in your monitor. -
Waiting in your monitors should be done using condition variables. Locks should be held only for short moments.
Getting started
Use the same project file organization as in lab 1. Copy Lab1.java
to Lab1Extra.java
, rename the class, and change Main.java
to use Lab1Extra
instead of Lab1
. You do not need to change the map file.
Tips and Tricks
To give you some hints, you should think that each track is represented by a monitor. The monitor will have, at least, the following interface:
where enter()
is called by a train when it enters a single track, and leave()
is called by a train when it leaves a single or double track.
Submission
In your submission, you should clearly explain the following items:
- What is the purpose of each method in the monitor?
- How many conditional variables are needed in your solution? Justify your answer.