The program allows Readers concurrent access to a resource while restricting Writers to exclusive access. Thread access to the shared resource is depicted by a light blue (cyan) arc segment. It is possible by starting the Reader threads at different times to create an execution in which Writer threads never get access to the resource.
When a Reader or Writer thread is paused, you can grab the start or end line of the critical section and move it around, thereby making the critical section larger or smaller. | ![]() |
Problem: when the readers overlap, the writers will starve.
A policy for reader writer implements the interface ReadWrite.java.
The applet (ReadersWriters.java)
instantiates a SafeReadWrite policy.
It uses a hook method ReadWrite makePolicy(ReaderWriterPanel view)
to instantiate the specific reader writer policy. You only need to overwrite
this method in subclasses.
Subclass SafeReadWrite.java
to make a policy where writers have priority. This class (say WritersPriorityReadWrite.java
)
does not allow new readers to enter when a writer is waiting.
Here is a solution where Writers have priority.
However the writer priority policy makes it possible for
readers to get starved out! Make a fair solution by subclassing your class
WritersPriorityReadWrite
to FairReadWrite
.
Hint: consider using a flag to switch writers priority on and off.
Here is a solution which is fair for Readers and Writers.