![]() |
![]() |
|
02158 Concurrent Programming Fall 2024 |
Lab 3: Basic Java Monitors |
Home | Plan | Material |
You should be familiar with the general notion of monitors as given in [Andrew 5-5.2] as well as Java's built-in monitor notion [Andrews 5.4], [Sync 5].
This lab is based on the playground setting known from Mandatory Assignment 2.
Now, a small shed has been built in the North West corner of the playgound, forcing cars no. 5-8 to change their path round the shed corner.
The full code for the lab is to be found in lab3.zip which you should download and integrate into your favourite IDE.
This time you are provided with an implemenation of the Field class which should not be changed. You are also given a dummy implementation of the Alley class. This is implemented by two skeleton classes BasicAlley and DoubleAlley which you are supposed to complete.
In the Alley class, only the create() factory method should be changed (in order to select the proper implemenation class).
The lab has three parts, A, B, and C.
Implement the methods enter() and leave() in BasicAlley.java such that its single instance becomes a monitor synchronizing the cars' access to the monitor. The solution should prevent deadlock in the alley and let cars in the same direction follow each other (as in Problem 5 of Mandatory Assignment 2).
This solution should not addresss the passing of the shed. Likewise, it should not try to avoid starvartion of cars in either direction.
In the basic alley solution, cars no. 1 and 2 will experience that they are prevented access to the alley until the last upgoing car has passed round the shed corner. Waiting that much does not seem necessary.
Addressing this issue, the alley may be seen as a double alley: An inner alley used by all cars (except no. 0, of course), and an outer alley (along the shed) used by cars no. 3-8 only.
To facilitate this view, the CarControl - in addition to calling enter() and leave() - at certain positions defined by the predicate atInnerExit() calls the method leaveInner() in the Alley class.
In the DoubleAlleyl.java class, the methods enter() and leave() as well as leaveInner() so that cars no. 1 and 2 get access to the alley as soon as the last upgoing car has passed their entry point.
Observe: Remember to switch to the DoubleAlley implemenation in the create() method of the Alley class.
[This problem will be part of Mandatory Assignment 3.]
The Gate class is given as an abstract class implemented by the semaphore-based solution found in SemGate.java.
Implement instead the Gate class as a Java monitor filling out the skeleton MonGate.java. such that it works like SemGate.
[Remember to change the factory method Gate.create() to use MonGate.]
Good luck!
Hans Henrik Løvengreen, Oct 27, 2024 |