8.1 It was asserted that the faults in the Dangerous
client were caused by two different interruptions. Show that this
is the case by implementing each synchronization
in turn.
8.2 The correct operation of a TrafficLight
instance depends upon its setOtherLight()
action being called before its run()
action, however it does not enforce this consideration. Re-implement
the run() action to throw
an exception if theOtherLight
instance attribute has not been initialised. Adapt the TrafficDemo
client to show the TrafficLight
class is now implemented in a secure manner.
8.3 Adapt the holdOn()
action to give a different delay depending upon the colour of
the light in order to make the simulation more realistic.
8.4 Attempt to demonstrate that a single Java statement can be
interrupted. To do this implement a DangerousWriter
class which either sets or clears all of its bits in an long
attribute in a single action. The action can be shown to be dangerous
if the value read from the class is not equal to the hexadecimal
values 0x00000000 or 0xFFFFFFFF.
Hint: if the value of the integer attribute, called dangerousInteger,
is 0 then all bits are currently clear and can be set by adding
0xFFFFFFFF, adding this a second time will clear all bits. More
details can be found in Appendix A1.
8.5 Design and implement a TrafficMonitor
class which uses a random number generator to indicate if any
traffic is waiting and if traffic is still flowing. Associate
a TrafficMonitor instance
with each TrafficLight
instance. Adapt the TrafficLight
class so that, when it is on green, it will continually
ask its TrafficMonitor
if traffic is still flowing and relinquish control as soon as
none is flowing. Demonstrate the effectiveness of the revision
using an unchanged TrafficDemo
client.
8.6 Further extend the TrafficLight class from exercise 8.5 so that whilst it is on green it asks its own TrafficMonitor for its traffic flow and asks the other TrafficLight for its traffic waiting. It should relinquish control as soon as its traffic flow is significantly lower than the traffic waiting at the other light. Demonstrate the effectiveness of the revision using an unchanged TrafficDemo client.